Skip to content

Instantly share code, notes, and snippets.

@toomasv
toomasv / primes.red
Last active May 4, 2024 08:41
Find primes up to given number
Red [Description: "Some prime funcs"]
primes: function [n [integer!]][
poke prim: make bitset! n 1 true
r: 2 while [r * r <= n][
repeat q n / r - 1 [poke prim q + 1 * r true]
until [not pick prim r: r + 1]
]
collect [repeat i n [if not prim/:i [keep i]]]
]
@toomasv
toomasv / make-block.red
Last active May 4, 2024 07:15
Transform data into block
make-block: function [data /all /with selection [word! block!]][
if word? selection [selection: to-block selection]
either any [
accessors: select system/catalog/accessors type?/word data
system/words/all [ ; NB! Modifies global "query"
url? data
data: decode-url data
accessors: words-of data
]
system/words/all [
@toomasv
toomasv / area-caret.red
Last active February 8, 2023 10:56
Example of getting and setting caret in area
Red [
Needs: 'View
License: "BSD-3"
]
;Mainly @hiiamboris
;Inserts closing `"])}` for `"[({` automatically
#system [
#import [
"user32.dll" stdcall [
SendMessage: "SendMessageW" [
@toomasv
toomasv / form-to.red
Last active November 3, 2022 06:33
Form number to given precision
Red [
Description: "Form number to given precision"
Date: 2019-0-13
]
form-to: func [
"Form number to a given floating-point precision"
number [number!] "Number to be formed"
precision [integer!] "Decimal places (positive integer)"
/local pcnt?
][
@toomasv
toomasv / wd-sparql.red
Last active October 6, 2022 19:48
Tiny Wikidata SPARQL
Red [
Description: {Playing while reading Wikidata SPARQL tutorial}
See: https://www.wikidata.org/wiki/Wikidata:SPARQL_tutorial
Also: https://stackoverflow.com/questions/38527828/how-to-query-wikidata-items-using-its-labels
Needs: View
Also-needs: https://github.com/toomasv/table-template
]
#include %table-template.red
ws: charset " ^-^/"
@toomasv
toomasv / add-face.red
Last active September 24, 2022 08:19
Toy face-adder
Red []
move-faces: function [parent except inserted dim][
anti: select [x y x] dim
win: parent/parent
max-size: 0x0
last-size: parent/size
foreach-face/with parent [
sz: either face/offset = except/offset [
except/size/:dim + 10
@toomasv
toomasv / to-num-word.red
Last active June 26, 2022 09:49
Spell out numbers
Red [
Description: "Spell out numbers"
]
context [
digits: [One Two Three Four Five Six Seven Eight Nine]
tens: [_ Twenty Thirty Fourty Fifty Sixty Seventy Eighty Ninety]
teens: [Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen]
orders: [Thousand Million Billion]
out: copy []
res: copy []
@toomasv
toomasv / puzzle.red
Last active June 25, 2022 19:20
Golovolomka
Red [
Description: "Puzzle"
See: "[June 22, 2022 9:56 PM](https://gitter.im/red/rethink?at=62b365e276cd751a2f507697)"
]
#include %to-num-word.red ;See next gist
prepare: func [puz] [
count: 0
total: length? reduce puzzle
clear res: []
op: func [op /local f x][
@toomasv
toomasv / bind-part.red
Created June 22, 2022 18:12
Bind slice of block
Red [
Needs: 'View
Problem: "[June 22, 2022 1:41 PM](https://gitter.im/red/red?at=62b2f1c8d3c8894f7198cf95)"
]
bind-part: function [what where with][
yep: false
beg: where/1
fin: where/2
parse what [any [s:
if (s/1 = fin) thru end
@toomasv
toomasv / thru.red
Last active February 22, 2022 07:18
Like `range`, but op!
Red [
Author: "Toomas Vooglaid"
Date: 2018-12-11
Improvements: @9214
]
thru: make op! func [a b /local inc op cmp][
inc: pick [1% 1] percent? a
set [op cmp] reduce pick [[:+ :<][:- :>]] a < b
collect [until [keep a b cmp a: a op inc]]
]