Skip to content

Alternative, underscore syntax for anonymous functions in Julia

License

Notifications You must be signed in to change notification settings

haberdashPI/LambdaFn.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LambdaFn

Project Status: Active – The project has reached a stable, usable state and is being actively developed. GitHub Actions Codecov PkgEval

This small package provides an alternative syntax for writing anonymous functions. It allows the use of three types of function arguments within the body of a call to or @lf:

  1. Implicit - each _ is replaced with a new function argument
  2. Numbered - each _[n] is replaced with the nth argument to the function
  3. Named - each _[name] is replaced with an argument named name, and they occur in the function argument list in the same order they appear in the function body.

Note that the three types of arguments cannot be mixed: @λ(_1 + _) throws an error.

How to type λ

Where supported (e.g. VSCode, Vim, Julia REPL, or Juno), you can type λ starting with a backward slash followed by the word lambda i.e. \lambda.

Examples

using LambdaFn

(_ + _) # == (x,y) -> x+y
(_a*_b + _a) # == (a,b) -> a*b + a
(_2 - _1) # == (_1,_2) -> _2 - _1
(_1 - _3) # == (_1,_2,_3) -> _1 - _3
filter((_.value > 10),data) # == filter(x -> x.value > 10,data)
1:10 |> (filter((_ > 3),_)) == 4:10

This macro resembles the syntax in this proposal, and I basically made this package because I got tired of waiting for that feature. The macro is a little more verbose than the proposed syntax change to julia, but I've grown to like the extra options it allows. I also like that it still has an explicit boundary around the body of the anonymous function, an issue that really complicates use of a bare _ as an anonymous function argument.

Alternatives

  • Underscore.jl lets you use a single macro applied over multiple functions and use the _ (or _1) syntax for anonymous functions. I think I like it better than this package.
  • LightQuery.jl has the @_ macro, which I know less about.
  • Lazy.jl has @>, @>> and @as.

About

Alternative, underscore syntax for anonymous functions in Julia

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages