You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Previously I was aiming to have three separate callable entities in Onyx: a generator block (=>), a closure-less function (->) and a closure-full lambda (~>).
Right now I'm considering using the only lambda functionality mimicking such in Typescript, i.e. simply => with auto-closure.
A compiler may implicitly optimize a lambda down to a coroutine, or to a pure function if there is no closure.
But in general case, a lambda is a class and thus can be passed around and auto-deleted.
import Scheduler from "std/scheduler.nx"
import { Mutex } from "std/threading.nx"
# Both are implicitly closured.
final list = new List<Int32>()
final mutex = new Mutex()
final promise = Scheduler.parallel(() => {
# The context here is implicitly `threadsafe`.
#
# list.push(42) # => Panic! Can't call `fragile List<Int32>::push`
# from within a threadsafe context!
mutex.sync(() => list.push(42)) # OK, the access is synchronized,
# `Mutex::sync` call is threadsafe
})
final sum = (a: Int32, b: Int32) => a + b
assert(sum(1, 2) == 3) # A compiler may optimize lambda out
# assert(1 + 2 == 3) # Like this
forall T def List<T>.each(yield: (T) =>): void {
let i = 0z
while (i < this.size) { yield(self[i]); i += 1 }
}
list.each(e => puts(e))
# May be optimized to this, eliminating lambda allocation:
let i = 0z
while (i < list.size) { puts(list[i]); i += 1 }
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Previously I was aiming to have three separate callable entities in Onyx: a generator block (
=>
), a closure-less function (->
) and a closure-full lambda (~>
).Right now I'm considering using the only lambda functionality mimicking such in Typescript, i.e. simply
=>
with auto-closure.A compiler may implicitly optimize a lambda down to a coroutine, or to a pure function if there is no closure.
But in general case, a lambda is a class and thus can be passed around and auto-deleted.
Beta Was this translation helpful? Give feedback.
All reactions