Skip to content

Instantly share code, notes, and snippets.

View greggirwin's full-sized avatar

Gregg Irwin greggirwin

  • Redlake Technologies
View GitHub Profile
@greggirwin
greggirwin / arity-of.red
Last active April 12, 2021 17:47
Red arity-of function
; We have other reflective functions (words-of, body-of, etc.), but
; this is a little higher level, sounded fun to do, and may prove
; useful as we write more Red tools. It also shows how to make your
; own typesets and use them when parsing.
arity-of: function [
"Returns the fixed-part arity of a function spec"
spec [any-function! block!]
/with refs [refinement! block!] "Count one or more refinements, and add their arity"
][
@greggirwin
greggirwin / _5-pane-view.red
Created March 7, 2021 18:41
5 Pane View GUI example
Red []
_5-panes-ctx: context [
layout-spec: none ; layout spec we build
p-top: p-left: p-center: p-right: p-bottom: none ; area panels
def-panel-sizes: [top 50 left 200 right 150 bottom 25]
sizes: [] ; panel sizes used in layout
win-sizes: compose [ ;!! may be better to separate default
Red []
context [
list: none
last-event: none
trap: function [
event [word!] ;-- event name
input [string! binary!] ;-- input series at current loading position
type [datatype! word! none!] ;-- type of token or value currently processed.
; Go lang style defer experiment
deferreds-ctx: context [
stack: []
cur-stack: does [last stack] ; current function's stack
push-frame: does [append/only stack copy []] ; new stack frame
pop-frame: does [take/last stack] ; drop last stack frame
;push-defer: func [blk [block!]] [append/only cur-stack blk] ; LIFO
push-defer: func [blk [block!]] [insert/only cur-stack blk] ; FIFO
do-deferreds: does [foreach blk cur-stack [attempt [do blk]]]
@greggirwin
greggirwin / profile.red
Last active December 28, 2020 06:17
Basic profiling for Red code comparisons
Red []
e.g.: :comment
delta-time: function [
"Return the time it takes to evaluate a block"
code [block! word! function!] "Code to evaluate"
/count ct "Eval the code this many times, rather than once"
][
ct: any [ct 1]
@greggirwin
greggirwin / select-case-tests.red
Created November 4, 2020 18:19
select-case.red (old experimental dialected dispatch func)
Red []
do %select-case.red
test: func [val] [print mold :val]
a: 15
test select-case a [15 [OK]]
test select-case a [1 [a] 5 [b] 15 [OK]]
@greggirwin
greggirwin / red-object-browser.red
Last active October 16, 2020 17:48
Red Object Browser
Red [
title: "Red Object Browser"
author: "Gregg Irwin"
needs: 'View
]
e.g.: :comment
map-ex: func [
"Evaluates a function for all values in a series and returns the results."
@greggirwin
greggirwin / view-flags-demo.red
Created July 10, 2020 21:46
View/flags demo, showing what window flags are available.
Red []
window-flags: [
resize no-title no-border no-min no-max no-buttons modal popup
]
show-win-using-flags: func [flags [block!]][
view/flags [backdrop sky button "OK" [unview]] flags
]
spec: [
below
@greggirwin
greggirwin / sorted.red
Created June 12, 2020 19:43
Minimal sorted series support, allowing fast binary searches.
Red [
comment: {
Minimal sorted series support, allowing fast binary searches.
I tinkered with a "dialected" interface, and more support
functions, like `[remove take at index?]` but quickly saw
that they were of little value, and that using a parameter
for the command was downright misleading to read, no matter
how clever the implementation. `Find` is not strictly
necessary either, but it does make it nicely consistent
@greggirwin
greggirwin / step.red
Created May 23, 2020 20:10
Old R2 `step` function, ported to Red, for incrementing alpha-numeric strings.
Red []
step-ctx: context [
digit=: charset [#"0" - #"9"]
alpha=: charset [#"A" - #"Z" #"a" - #"z"]
alpha-num=: union alpha= digit=
; Could do this with charsets.
range-start-char?: func [val] [to logic! find "0Aa" val]