Code Monkey home page Code Monkey logo

Comments (1)

ozra avatar ozra commented on August 24, 2024

The simplest way is without a doubt enclosing the lambda expression in parentheses:
((x Str) -> say x)("Yo anonymous!"), and then calling with parentheses syntax for the arguments.

Note that fragments do not need typed parameters, because they are invoked in a given context where the types can be inferred, and thus they can't be used stand alone (unless typed into a lambda).

Lambdas however, that can be used "stand alone", and passed around, need to have their arguments typed. This is because (unless you use closured variables in it) it is a C-compatible function (pointer), and because there's no telling where it will be passed.

Thus ((x) ~> say x)("Yo anonymous!") will not work:

can't execute `((x)~>
  $.raise("can't execute `say(x)` at /tmp/foo.ox:1:9: `x` has no type")
-- my-lambda is a neat and tidy super compatible first class function
my-lambda = (x Str) -> y = stuff x; do-more-stuff y
my-lambda "foo"

-- arg is _yielded_ to the fragment, and type inferred in the process:
some-fun-taking-fragment (x) ~> y = stuff x; do-more-stuff y  

It might be possible to implement type inference for when calling a lambda and/or fragment immediately, not sure how hard it would be, probably some crazy edge cases...

The "man-or-boy" compiler challenge (which exist only to terrorize compilers) shows that Onyx is a man-compiler, but, unfortunately self-reference is not possible in the lambda-body without declaring it before (b = raw () -> T) :-/

-- "man-or-boy.ox"
a(k T, x1, x2, x3, x4, x5) ->
   b = raw () -> T
   if k <= 0 ? x4() + x5() : (b = () -> k -= 1; a k, b, x1, x2, x3, x4)()

say a 10, () -> 1, () -> -1, () -> -1, () -> 1, () -> 0

I think it's quite a bit harder then it's worth. How big portion of your code is actually made out of such acrobatics?

from onyx-lang.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.