Browse Source

Merge pull request #231 from 01-edu/listforeachif-fix

fix listforeachif readme
content-update
LEEDASILVA 5 years ago committed by GitHub
parent
commit
4a5e33fd34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 69
      subjects/listforeachif.en.md

69
subjects/listforeachif.en.md

@ -1,4 +1,4 @@
## listpushback ## listforeachif
### Instructions ### 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. - `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. - `cond` 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`. - The function given as argument must have a pointer as argument: `*NodeL`.
@ -25,11 +25,37 @@ type List struct {
Tail *NodeL 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 package main
import ( import (
"fmt"
piscine ".." piscine ".."
"fmt"
) )
func PrintElem(l *List) { func PrintElem(node *piscine.NodeL) {
fmt.Println(l.Head.Data) fmt.Println(node.Data)
} }
func StringToInt(l *List) { func StringToInt(node *piscine.NodeL) {
count := 1 node.Data = 2
l.Head.Data = count
} }
func PrintList(l *List) { func PrintList(l *piscine.List) {
m := l.Head it := l.Head
for m != nil { for it != nil {
fmt.Print(m.Data, " -> ") fmt.Print(it.Data, "->")
m = m.Next it = it.Next
} }
fmt.Println()
fmt.Print(l.Tail)
} }
func main() { func main() {
link := &List{} link := &piscine.List{}
piscine.ListPushBack(link, 1) piscine.ListPushBack(link, 1)
piscine.ListPushBack(link, "hello") piscine.ListPushBack(link, "hello")
@ -75,16 +100,16 @@ func main() {
piscine.ListPushBack(link, "!") piscine.ListPushBack(link, "!")
piscine.ListPushBack(link, 54) piscine.ListPushBack(link, 54)
PrintAllList(link) PrintList(link)
fmt.Println() fmt.Println()
fmt.Println("--------function applied--------") 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--------") fmt.Println("--------function applied--------")
PrintAllList(link) PrintList(link)
fmt.Println() fmt.Println()
} }

Loading…
Cancel
Save