Skip to content

Instantly share code, notes, and snippets.

@nedzadarek
nedzadarek / set-in-function.red
Created July 6, 2018 08:46
`set` used in the function doesn't make it local
f1: function [][parse [1] [set n integer!]]
; func [][parse [1] [set n integer!]]
f1
; true
n
; 1
f2: function [][set 'm 1]
; func [][set 'm 1]
f2
s: {^/ 2^/ 2^/ 3}
heredoc2: function [str [ string!] ][
str: remove copy str
number-of-spaces-in-line: spaces: 0 ; initialize in order not to leak by parse
lines: split str "^/"
indentation: 99999999
foreach line lines [
parse line [copy spaces any space (number-of-spaces-in-line: length? spaces) to end]
if indentation > number-of-spaces-in-line [
indentation: number-of-spaces-in-line
@nedzadarek
nedzadarek / random-bitset.red
Created July 5, 2018 11:34
random set bit from bitset
b: make bitset! "ajfnxiermnpqpau"
; b: make bitset! "z"
solution1: [
; make bitset! #{0000000000000000000000004A3810}
bl: copy []
; []
repeat w (length? b) [
if b/:w [append bl w]
]
; bl
@nedzadarek
nedzadarek / cached-function.red
Last active July 3, 2018 14:34
Cached functions with "series in the function" feature.
Red [
author: "Nędza Darek"
license: {
Just mention me/link to gist/github.
}
version: 0.0.1
]
; not cached:
long-fact: func [
n
@nedzadarek
nedzadarek / afunc.red
Created June 25, 2018 13:42
Adds support for `op!`/`function!` based heavily on http://red.qyz.cz/dependent-types.html
Red [
info: {
Adds support for `op!`/`function!` based heavily on http://red.qyz.cz/dependent-types.html
}
]
my-assert: func [bl] [
unless true = do bl [cause-error 'user 'message ["Wrong assertion!!"]]
]
afunc: func [
"Make function with more checks"
@nedzadarek
nedzadarek / from-tuple-to-block.red
Created May 21, 2018 23:33
from tuple to block
Red [
author: "Nędza Darek"
license: {
- use/modify everywhere
- point to this gist
}
url: https://gitter.im/red/help?at=5b0301c3d245fe2eb7ca406c
info: {
Code that I used:
`profile`: https://gist.github.com/greggirwin/908d44dc069ed84cf69f053e1308390d
; https://gitter.im/red/help?at=5afd5cecf04ce53632e4ae07
; ex: [block! paren! path! lit-path! set-path! get-path! hash!
; image! ; only tuple!
; vector! binary! ] ; support not all data
; series1: [block! paren! string! file! url! path! lit-path! set-path!
; get-path! vector! hash! binary! tag! email! image!]
; any-string1: [string! file! url! tag! email!]
collect: func [
; rea: make deep-reactor! [
rea: object [
a: [1 2 3 4 5]
s: "abbccbba"
on-deep-change*: func [
owner
word
target
action
new
@nedzadarek
nedzadarek / each-from-doing.red
Created May 6, 2018 15:54
<ACTION> X from Y doing Z
; more info here:
; https://gitter.im/red/red?at=5aef1a635cf0b830045e6264
from: make op! func [a b] [reduce [a b]]
each: make op! func ['a b] [reduce [a b]]
keep each [x y] from [1 2 3 4]
; == [[keep [x y]] [1 2 3 4]]
@nedzadarek
nedzadarek / to-new-line-marker.red
Created May 2, 2018 14:33
Parse lines (to new-line marker)
bl: [a b
c
]
parse bl [any [pos: if (not new-line? pos) skip] 'c]
; == true
bl: [ a b -> a + b
c d -> c * (c + d)
]