Congrats on @haxscramper for the very nice new blogpost on Pattern matching in Nim! (there is also a HN post up for discussion)
https://nim-lang.org/blog.html
Since this is available through fusion I have a couple of questions: if I wanted to have a nimble package to depend on something in fusion, what should I do? what are the versioning guarantees?
My current understanding is:
I am asking because:
Your understanding for requires "fusion" is correct as of now, but in future there are plans to start adopting semantic versioning for fusion and make it behave like any other 'normal' package. See https://github.com/nim-lang/Nim/pull/16925
I personally think that idea with automatically bundling fusion was not so bad, but since it is no longer how it works it would make sense to avoid any sort of special treatment for 'fusion-the-package', and make it work like any other.
beautiful work, @haxscramper, especially the documentation! the error messages are so inscrutable i couldn't have made any progress without it.
generics require you to wrap the matching code in a template(foo:typed)
here's my red black tree on RosettaCode as an example
Looking at the code, and comparing it with the OCaml right below it (I used to code in OCaml) and the Rust later (I coded in Rust for a while, back to C++ now) I perceive the elegance of the solutions with the pattern matching built into the language greater than the Nim solution.
I understand, it's just like, my opinion man, and all that. I hope that if there's ever a Nim 2.0, it follows Rust and Swift and adopts pattern matching and algebraic data types.
I perceive the elegance of the solutions with the pattern matching built into the language greater than the Nim solution.
+1, something like
let [(first, second), trail...] = [(12, 3), (33, 4), (12, 33)]
Looks much better than code with special characters like @ and :=
[(@first, @second), all @trail] := [(12, 3), (33, 4), (12, 33)]
I perceive the elegance of the solutions with the pattern matching built into the language greater than the Nim solution.
I thought the post was clear about that -- we're still improving the design. Maybe pattern matching will make it into Nim's core too, who knows.
i agree and disagree:
having [T] everywhere and the extra templates to fake the Haskell/Ocaml/F# syntax makes it look really cluttered, and I would prefer the more verbose, default, 2D syntax for clarity over elegance.
proc balance(t: var RBTree) =
template res = t = B(R(a,x,b),y,R(c,z,d))
case t
of (colour: Black,
left: (colour: Red,
left: (colour: Red,
left: @a,
value: @x,
right: @b),
value: @y,
right: @c),
value: @z,
right: @d): res
of (colour: Black,
left: (colour: Red,
left: @a,
value: @x,
right: (colour: Red,
left: @b,
value: @y,
right: @c)),
value: @z,
right: @d): res
of (colour: Black,
left: @a,
value: @x,
right: (colour: Red,
left: (colour: Red,
left: @b,
value: @y,
right: @c),
value: @z,
right: @d)): res
of (colour: Black,
left: @a,
value: @x,
right: (colour: Red,
left: @b,
value: @y,
right: (colour: Red,
left: @c,
value: @z,
right: @d))): res
which is clunky but it depends what you're trying to communicate.
When it comes to the inject function, I think it's better in Nim, it tells a clear story:
case t
of (colour: Empty):
t = R(E,x,E)
of (value: > x):
t.left.ins(x)
t = balance(t)
of (value: < x):
t.right.ins(x)
t = balance(t)
It's all about the relative weighting in the (efficiency,expressivity,elegance) tuple :)
Is there a compiler flag needed to compile it with fusion (installed via nimble) ?
I get an error in the line of the case..: Error: attempting to call undeclared routine: 'case' when I try to compile:
import fusion/matching
{.experimental: "caseStmtMacros".}
case [(1, 3), (3, 4)]:
of [(1, @a), _]:
echo a
else:
echo "Match failed"
What's you nim version?
nim -v