|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
## listpushback |
|
|
|
|
## listforeachif |
|
|
|
|
|
|
|
|
|
### Instructions |
|
|
|
|
|
|
|
|
@ -8,7 +8,7 @@ Write a function `ListForEachIf` that applies a function given as argument to th
|
|
|
|
|
|
|
|
|
|
- `f` is a functions that is applied to the node. |
|
|
|
|
|
|
|
|
|
- `comp` is a predicate (a function that returns true or false) and will be use to determine if the function `f` would be applied to the node. |
|
|
|
|
- `` is a predicate (a function that returns true or false) and will be use to determine if the function `f` would be applied to the node. |
|
|
|
|
|
|
|
|
|
- The function given as argument must have a pointer as argument: `*NodeL`. |
|
|
|
|
|
|
|
|
@ -25,11 +25,37 @@ type List struct {
|
|
|
|
|
Tail *NodeL |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func CompStr(l *NodeL) bool { |
|
|
|
|
func IsPositive_node(node *NodeL) bool { |
|
|
|
|
switch node.Data.(type) { |
|
|
|
|
case int, float32, float64, byte: |
|
|
|
|
return node.Data.(int) > 0 |
|
|
|
|
case string, rune: |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func IsNegative_node(node *NodeL) bool { |
|
|
|
|
switch node.Data.(type) { |
|
|
|
|
case int, float32, float64, byte: |
|
|
|
|
return node.Data.(int) > 0 |
|
|
|
|
case string, rune: |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func IsNotNumeric_node(node *NodeL) bool { |
|
|
|
|
switch node.Data.(type) { |
|
|
|
|
case int, float32, float64, byte: |
|
|
|
|
return false |
|
|
|
|
case string, rune: |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func ListForEachIf(l *List, f func(*NodeL), comp func(*NodeL) bool) { |
|
|
|
|
func ListForEachIf(l *List, f func(*NodeL), cond func(*NodeL) bool) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
@ -42,30 +68,29 @@ Here is a possible [program](TODO-LINK) to test your function :
|
|
|
|
|
package main |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"fmt" |
|
|
|
|
piscine ".." |
|
|
|
|
"fmt" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func PrintElem(l *List) { |
|
|
|
|
fmt.Println(l.Head.Data) |
|
|
|
|
func PrintElem(node *piscine.NodeL) { |
|
|
|
|
fmt.Println(node.Data) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func StringToInt(l *List) { |
|
|
|
|
count := 1 |
|
|
|
|
l.Head.Data = count |
|
|
|
|
func StringToInt(node *piscine.NodeL) { |
|
|
|
|
node.Data = 2 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func PrintList(l *List) { |
|
|
|
|
m := l.Head |
|
|
|
|
for m != nil { |
|
|
|
|
fmt.Print(m.Data, " -> ") |
|
|
|
|
m = m.Next |
|
|
|
|
func PrintList(l *piscine.List) { |
|
|
|
|
it := l.Head |
|
|
|
|
for it != nil { |
|
|
|
|
fmt.Print(it.Data, "->") |
|
|
|
|
it = it.Next |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fmt.Print(l.Tail) |
|
|
|
|
fmt.Println() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
|
link := &List{} |
|
|
|
|
link := &piscine.List{} |
|
|
|
|
|
|
|
|
|
piscine.ListPushBack(link, 1) |
|
|
|
|
piscine.ListPushBack(link, "hello") |
|
|
|
@ -75,16 +100,16 @@ func main() {
|
|
|
|
|
piscine.ListPushBack(link, "!") |
|
|
|
|
piscine.ListPushBack(link, 54) |
|
|
|
|
|
|
|
|
|
PrintAllList(link) |
|
|
|
|
PrintList(link) |
|
|
|
|
|
|
|
|
|
fmt.Println() |
|
|
|
|
fmt.Println("--------function applied--------") |
|
|
|
|
piscine.ListForEachIf(link, PrintElem, CompStr) |
|
|
|
|
piscine.ListForEachIf(link, PrintElem, piscine.IsPositive_node) |
|
|
|
|
|
|
|
|
|
piscine.ListForEachIf(link, StringToInt, CompStr) |
|
|
|
|
piscine.ListForEachIf(link, StringToInt, piscine.IsNotNumeric_node) |
|
|
|
|
|
|
|
|
|
fmt.Println("--------function applied--------") |
|
|
|
|
PrintAllList(link) |
|
|
|
|
PrintList(link) |
|
|
|
|
|
|
|
|
|
fmt.Println() |
|
|
|
|
} |
|
|
|
|