Arc Forumnew | comments | leaders | submitlogin
1 point by Pauan 4434 days ago | link | parent

List splicing now works:

  > (var (list a (splice b) c) (list 1 2 3 4))
  a -> 1
  b -> (2 3)
  c -> 4
Unfortunately it hardcodes the symbol "splice" but I plan to change that. Now to work on dictionary splicing... Oh, and by the way, once I get the reader working, the above will look like this:

  (var {a @b c} {1 2 3 4})
Much nicer, no?


1 point by Pauan 4434 days ago | link

Dictionary splicing works. It took a mere 7 minutes to add it in:

https://github.com/Pauan/nulan/commit/d83d94b10513fbd471f5ed...

See how easy that is?! But the best part is that this customizable pattern matching is available to Nulan. The implementation is decoupled from the interface, because I'm using objects.

Anyways, how dictionary splicing works is...

  > (var [@a "foo" b] ["foo" 1 "bar" 2 "qux" 3])
  a -> ["foo" 1 "bar" 2 "qux" 3]
  b -> 1


  > (var ["foo" a @b] ["foo" 1 "bar" 2 "qux" 3])
  a -> 1
  b -> ["bar" 2 "qux" 3]
What happened is that in the first one, it took the entire dictionary and bound it to "a", then it bound the "foo" key to "b".

In the second one, it bound the "foo" key to "a", and then bound everything except the "foo" key to "b".

Thus, depending on where you splice, you get different results. The first one is like Haskell's "as" patterns:

http://zvon.org/other/haskell/Outputsyntax/As-patterns_refer...

The second one is the more traditional "put everything except these keys into this variable" approach. And of course you can combine them as much as you like:

  > (var ["foo" a @b "bar" 2 @c] ["foo" 1 "bar" 2 "qux" 3])
  a -> 1
  b -> ["bar" 2 "qux" 3]
  c -> ["qux" 3]
And for fun, you can nest things inside the splice too:

  > (var ["foo" a @["bar" b]] ["foo" 1 "bar" 2 "qux" 3])
  a -> 1
  b -> 2
The above is exactly the same as (var ["foo" a "bar" b] ["foo" 1 "bar" 2 "qux" 3]) except that it needed to do a bit more work. Useful? Who knows, but it's awesome.

---

By the way, for the sake of clarity, I used Nulan notation for all the above stuff. But the reader isn't implemented yet, so for now you have to use (dict ...) rather than [...] and (splice ...) rather than @...

-----

1 point by Pauan 4434 days ago | link

[] {} and @ syntax now works:

  (var {a b @c} {1 2 3 4})
  a -> 1
  b -> 2
  c -> (3 4)
In addition, it no longer hardcodes the "splice" symbol, but now the only way to splice is to use @. I plan to fix this so that you can use both in a hygienic way.

-----