Skip to content

Multiple distinct objects

editable example

Click the pencil to open this in an editor, change it, and run it in your browser. The same solution is posted on Rosetta Code.

ghul
use IO.Std.write_line
use Ghul.Pipes
class BOX(value: int public)
let n = 5
let distinct = (0..n) |> map(_ => BOX(0)) |> collect_list()
distinct[0].value = 42
write_line(
"distinct: {distinct |> map(box => "{box.value}") |> join(" ")}")
let shared = BOX(0)
let same = (0..n) |> map(_ => shared) |> collect_list()
same[0].value = 42
write_line(
"shared: {same |> map(box => "{box.value}") |> join(" ")}")
distinct: 42 0 0 0 0
shared:   42 42 42 42 42