Skip to content

Instantly share code, notes, and snippets.

@nedzadarek
Last active November 10, 2019 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nedzadarek/bb3614f8ee8d53bc6bbff21110ab4d73 to your computer and use it in GitHub Desktop.
Save nedzadarek/bb3614f8ee8d53bc6bbff21110ab4d73 to your computer and use it in GitHub Desktop.
Red [
author: "Nędza Darek"
date: 2019-11-09
license: {
- no warranties
- use/modify everywhere
- just mention (this gist/github or something like this)
}
version: #0.0.1.preview2
updates: {
2019-11-10 - version #0.0.1.preview2: fix: function doesn't return body
}
]
func': :func
func: func' [spec body] [
func'
append spec [/block_given? implicit-block]
body
]
f: func [a /foo baz] [
probe (a + 1)
if foo [print ['baz-argument baz] ]
if block_given? [
print 'block_given!
probe implicit-block
]
]
? f
; no refinement support; only for `function!` type (user defined in the Red)
implicity: func [body [block!]] [
last-function-position: body
parse body [
any [
set word word-position: word! if(
gotten-word: unless error? try [get word] [
function! = type? get word
]
) (
last-function-position: word-position
last-word: word
)
| set word word-position: path! if (
gotten-word: unless error? try [get first word] [
function! = type? get first word
]
)(
last-function-position: word-position
last-word: word
)
| lambda-symbol-position: '-> lambda-position: block! (
change last-function-position reduce[
either path? last-word [
append last-word 'block_given?
][
to-path reduce [last-word 'block_given?]
]
]
remove word-position
)
| skip
]
]
return head body
]
implicity bl: [f 1 -> [2 3 4] ]
do probe head bl
implicity bl_path: [f/foo 1 "baz-argument" -> [2 3 4] ]
do probe head bl_path
; further examples
prices: [scissor: 22 window: 33.4 mobile-phone: 50 calendar: 10]
get-prices: func [
"return a list of prices"
/price_limit limit "limit the price to the 'limit'"
][
print 'get-pricesss
either block_given? [
either price_limit [
limited-prices: copy []
foreach [name price] implicit-block [
if price <= limit [
append limited-prices name
append limited-prices price
]
]
return probe limited-prices
][
return implicit-block
]
][
do make error! "no shob object"
]
]
print 'prices
implicity bl: compose/only [get-prices -> (prices)] probe bl do bl
implicity bl2: compose/only [get-prices/price_limit 34 -> (prices)] probe bl2 do bl2
Red [
author: "Nędza Darek"
date: 2019-11-09
license: {
- no warranties
- use/modify everywhere
- just mention (this gist/github or something like this)
}
version: #0.0.1.preview1
updates: {
2019-11-10 - version #0.0.1.preview1: fix: function doesn't return body
}
]
func': :func
func: func' [spec body] [
func'
append spec [/block_given? implicit-block]
body
]
f: func [a] [
probe (a + 1)
if block_given? [
print 'block_given?
probe implicit-block
]
]
? f
; no refinement support; only for `function!` type (user defined in the Red)
implicity: func [body [block!]] [
last-function-position: body
parse body [
any [
set word word-position: word! if(
gotten-word: unless error? try [get word] [
function! = type? get word
]
) (
last-function-position: word-position
last-word: word
)
| lambda-symbol-position: '-> lambda-position: block! (
probe 'lambda
probe last-function-position
change last-function-position reduce[
to-path reduce [last-word 'block_given?]
]
remove word-position
)
| skip
]
]
return head body
]
implicity bl: [f 1 -> [2 3 4] ]
probe head bl
do bl
@nedzadarek
Copy link
Author

Ruby-like implicit block. Using function-name args ... -> block syntax.
func -> changed to have /block_given? implicit-block - same trick as with /local refinement
implicity -> if it sees -> block it will change function word/path to <function's word or path>/block_given? ... args ... block. Where ... args ... ` are arguments.

It works but expect some bugs. It's just a preview. main_without_path.red is simpler to understand but doesn't have path support.
It works with only function! for simplicity.

@nedzadarek
Copy link
Author

2019-11-10
Bigger description for @GiuseppeChillemi:
In the Ruby language, every function takes additional argument as a lambda (block of code). You don't have to specify it in the arguments' list. It's used for executing some code. It's the last element so it's useful when you want something at the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment