deathbeam / spoon

:ramen: Spoon is a programming language that runs blazingly fast, compiles to native code and works everywhere.
https://spoonlang.org
MIT License
56 stars 11 forks source link

Closures syntax #16

Closed deathbeam closed 8 years ago

deathbeam commented 8 years ago

Right now, we have pretty syntax for closures

def myClosure = (event) =>
  trace(event)
end

but imagine it in Haxe maps:

def myMap = [
  "myEvent" => (event) =>
    trace(event)
  end,
]

We also cannot use -> because it is used in Haxe when defining types for typed function variables.

francescoagati commented 8 years ago

i thinks that we need short lambda syntax for raxe. In ruby there is short lambda syntax with ->

twice = -> (x) { 2 * x }

see (http://makandracards.com/makandra/17305-short-lambda-syntax-in-ruby-1-9)

but for me is very ugly in this mode and many ruby programmers use simply lambda or proc for define a block. and in ruby many block are consumed at the end of the call method.

[1,2,3,4,5].map {|x| x + 1}

or for multi line blocks

[1,2,3,4,5].map do | x|
   x+1
end

so at the end short lambda in ruby isn't much used for my opinion. for me the best solution is to come to use "do"

for me the old sintax isn't ugly

self class

def self.main()
  def numbers = [1, 3, 5, 6, 7, 8]

  trace(numbers.count()) -- 6
  trace(numbers.has(4)) -- false

  -- test if all numbers are greater/smaller than 20
  trace(numbers.foreach(do(v)
    return v < 20
  end)) -- true

  trace(numbers.foreach(do(v)
    return v > 20
  end)) -- false

  -- sum all the numbers
  def sum = do(num, total) 
    return total += num
  end

  trace(numbers.fold(sum, 0)) -- 30
end

do is used also in tink_lang (https://github.com/haxetink/tink_lang#do-procedures)

francescoagati commented 8 years ago

but an alternative for use short lambda should be ==> or -->

francescoagati commented 8 years ago

here there are many sintax for short lambdas https://medium.com/@ncannasse/haxe-and-short-lambdas-c1f360f7c7c