Hello, Shea,

I don't think there are any plans for this, specifically.  It could,
however, mostly be done now with macros.  (The biggest missing part is that
`where` can't be paired with `end` right now, so you'd still need a `begin`
after the where.)

As an aside, if you haven't seen it yet, you might be interested in
PatternDispatch.jl, which addresses dispatch on values.

Cheers,
   Kevin

On Thu, Jan 15, 2015 at 2:56 PM, Shea Valentine <[email protected]>
wrote:

> Any chance 'where' clauses are in the pipeline?  It would pretty much just
> be syntactic sugar, where the code preceding where would be the same as the
> last line of a 'begin ... end' block.  It would obviously be welcome for
> people coming from languages like Haskell, and is arguably prettier.
>
> Examples:
> sorter(x::Array) = begin
>   if isempty(x)
>     return x
>   else
>     pivot = x[1]
>     smaller = filter(i-> i < pivot, x[2:end])
>     larger = filter(i-> i >= pivot, x[2:end])
>   end
>   [sorter(smaller), pivot, sorter(larger)]
> end
>
> Would become
> sorter(x::Array) = [sorter(smaller), pivot, sorter(larger)] where
>   if isempty(x)
>     return x
>   else
>     pivot = x[1]
>     smaller = filter(i-> i < pivot, x[2:end])
>     larger = filter(i-> i >= pivot, x[2:end])
>   end
> end
>
> Which makes the first line express the core functionality, and the code in
> the 'where' block details of that functionality.
> More interestingly
> function derivative(f)
>     return function(x)
>         h = x == 0 ? sqrt(eps(Float64)) : sqrt(eps(Float64)) * x
>         xph = x + h
>         dx = xph - x
>         f1 = f(xph)
>         f0 = f(x)
>         return (f1 - f0) / dx
>     end
> end
>
> could be written as
> derivative(f) = derived where
>     derived(x) = (f1 - f0) / dx where
>         h = x == 0 ? sqrt(eps(Float64)) : sqrt(eps(Float64)) * x
>         xph = x + h
>         dx = xph - x
>         f1 = f(xph)
>         f0 = f(x)
>     end
> end
>
> Which seems like a more natural way to higher-order function when using
> the 'f(x)=' idiom.
>
> Finally, mixed with a magical future with pattern matching (dispatch on
> scalars) we could have code like this:
> sorter([]) = []
> sorter(x::Array) = [sorter(smaller), pivot, sorter(larger)] where
>   pivot = x[1]
>   smaller = filter(i-> i < pivot, x[2:end])
>   larger = filter(i-> i >= pivot, x[2:end])
> end
>

Reply via email to