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 / 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 / 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 / get-refinements.red
Last active February 4, 2022 07:23
Get overview of refinements' usage
Red [
Purpose: "Get overview of refinement usage"
Date: 15-Nov-2021
Update: 4-Feb-2022
Author: @toomasv
]
context [
refs: make block! 100
add-ref: func [ref fn][
either got: refs/:ref [append got fn][repend refs [ref compose [(fn)]]]
@toomasv
toomasv / tic-tac-toe.red
Last active September 24, 2021 03:28
TIc-Tac-Toe
Red [
Description: "Tic-Tac-Toe"
Date: 2021-02-26
]
wins: [[1 2 3][4 5 6][7 8 9][1 4 7][2 5 8][3 6 9][1 5 9][3 5 7]]
check: func [move][
foreach win wins [
if 3 = length? intersect win state/:move [return win]
]