These are chat archives for evhub/coconut

Apr 2018
Michael Lamparski
Apr 13 2018 20:56
I think it would be really cool to have some sort of way to use functions in patterns. Something like this could be used to create "pattern aliases" and combinators
# map a dictionary
def dmap(f, d) = {k: f(v) for (k, v) in d.items()}

# make/typecheck an ndarray at runtime
def Nd(n=None, dtype=None) =
    def inner(arr) =
        arr = np.array(arr)
        if n is not None and arr.ndim != n:
            raise MatchError
        if dtype is not None and arr.dtype != dtype:
            raise MatchError

# totally strawman syntax.  (expr @> pat) is a pattern that evaluates expr (which should
#  be a function), call it on the value, and match it with `pat`
arr = {"a": [[1, 0]]} |> dmap((Nd(2) @> x) -> x.sum(axis=0)) # ok; ndim == 2
arr = {"a": [1, 0]} |> dmap((Nd(2) @> x) -> x.sum(axis=0)) # match error; ndim == 1
Er, well, okay, "combinators" is vague (it's not like you can have "patterns of patterns"), but it lets you take a combinator-based approach to certain things like runtime validation
Michael Lamparski
Apr 13 2018 21:03
There are if guards in function definition which let you do a more limited form of this, but even those are only supported in block-style functions (which I assume is due to grammatical limitations?)
Elliott Indiran
Apr 13 2018 22:17
I just opened a PR for #406. If that looks like a reasonable version of the solution you were looking for, I would be happy to add stub files for any of the other coconut modules people might import.