Skip to content

Commit

Permalink
impl[T: ToText] List[T]: ToText
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasWanke committed Dec 5, 2024
1 parent 86ccbe1 commit 223caa4
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages_v5/example.candy
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@ fun map[T, R](list: List[T], transform: (T) R) List[R] {
result.append(transform(item))
})
}
impl[T: ToText] List[T]: ToText {
fun toText(self: List[T]) Text {
let items = self.map((item: T) { item.toText() })
.reduce((result: Text, item: Text) { "{result}, {item}" })
.unwrapOr("")
"[{items}]"
}
}

struct MyStruct {
name: Text,
Expand Down Expand Up @@ -611,6 +619,8 @@ fun main() Int {

repeat(3, () { print("Hello, World!") })

print(listOf(1, 2, 3).map((x: Int) { x.multiply(2) }))

0
}

Expand Down

0 comments on commit 223caa4

Please sign in to comment.