aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornodist <kevin.comas.git@gmail.com>2026-08-02 11:07:19 -0400
committernodist <kevin.comas.git@gmail.com>2026-08-02 11:07:19 -0400
commit651cec72681afc0821a03a71d07aaa797d37649e (patch)
tree3a5a1689057052ad0fefcebac5ed8b651e5a56b4
parentc9d9511e6c964853e35f143cf45f3a0f02521e1d (diff)
new syntax
-rw-r--r--ack.kpl26
-rw-r--r--fib.kpl50
-rw-r--r--fib_import.kpl9
-rw-r--r--letter_table.kpl30
-rw-r--r--shared_counter.kpl57
-rw-r--r--sieve.kpl78
6 files changed, 31 insertions, 219 deletions
diff --git a/ack.kpl b/ack.kpl
deleted file mode 100644
index bba01ea..0000000
--- a/ack.kpl
+++ /dev/null
@@ -1,26 +0,0 @@
-
-// Ackermann function
-
-`export ack : ([I64; I64.m; I64.n]
- ? {
- |(m < 0; n < 0) { 0 }
- m = 0 { n + 1 }
- &(m > 0; n = 0) { ack `sync (m - 1; 1) }
- { ack `sync (m - 1; ack `sync (m; n - 1)) }
- }
-)
-
-`export ack_string : ([String; I64.m; I64.n]
- "#MAGENTA#ack#(#CYAN#%#, #CYAN#%#) #WHITE#=# #GREEN#%#\n" `format (m; n; ack `sync (m; n))
-)
-
-`is_main ([Void]
- `use "sys" [args]
- ? 4 != `length args {
- `return `error String $ (
- "#BOLD#RED#Got: %#\n" `format " " `join args
- "#BOLD#WHITE#Usage: % % <m> <n>#\n" `format (args `get 0; args `get 1)
- )
- }
- `value ack_string `sync (I64 $ args `get -2; I64 $ args `get -1)
-)
diff --git a/fib.kpl b/fib.kpl
index e1ab025..d8a2140 100644
--- a/fib.kpl
+++ b/fib.kpl
@@ -1,27 +1,39 @@
-/*
- Fibonacci sequence
-*/
+// Fibonacci sequence
-`export fib : ([I64; I64.n]
+`export fib_recursive : {[I64.n]
? {
- n <= 0 { 0 }
+ n < 0 { %.error ~ "#BOLD#RED#Invalid n Got %#" `format n }
+ n = 0 { 0 }
n < 2 { 1 }
- { `sync(fib; n - 1) + fib `sync n - 2 }
+ { + (fib `sync n - 1; fib `sync n - 2) }
}
-)
+}
-`export fib_string : ([String; I64.n]
- "#MAGENTA#fib#(#CYAN#%#) #WHITE#=# #GREEN#%#\n" `format (n; fib `sync n)
-)
+`export fib_iterative : {[I64.n]
+ ? n < 0 {
+ `return.error ~ "#BOLD#RED#Invalid n Got %#" `format n
+ }
+ a : 0
+ b : 1
+ @ 0 .. n {
+ c : b
+ b : a + b
+ a : c
+ }
+ a
+}
-`is_main ([Void]
- `use "sys" [args]
- ? 3 != `length args {
- `return `error String $ (
- "#BOLD#RED#Got: %#\n" `format " " `join args
- "#BOLD#WHITE#Usage: % % <n>#\n" `format (args `get 0; args `get 1)
- )
+`export fib_array : {[I64.n]
+ ? {
+ n < 0 {
+ `return.error ~ "#BOLD#RED#Invalid n Got %#" `format n
+ }
+ n > 0 { n :- 1 }
+ }
+ arr : Array $ (0; 1)
+ @ 0 .. n {
+ arr `push + (arr `get -2; arr `get -1)
}
- `value fib_string `sync I64 $ args `get -1
-)
+ arr
+}
diff --git a/fib_import.kpl b/fib_import.kpl
deleted file mode 100644
index 6f4e055..0000000
--- a/fib_import.kpl
+++ /dev/null
@@ -1,9 +0,0 @@
-
-// Import and run fib_string function
-
-`use "file" [write; stdout]
-`import "./fib.kpl" [fib_string]
-
-@ 0 .. 30 {[n]
- write `sync (stdout; fib_string `sync n)
-}
diff --git a/letter_table.kpl b/letter_table.kpl
deleted file mode 100644
index ad447be..0000000
--- a/letter_table.kpl
+++ /dev/null
@@ -1,30 +0,0 @@
-
-// Read a utf8 string and count all the occurrences of each letter found
-
-`export letter_table : ([String.string]
- table : Map[Char; U64] $ ()
- @ string {[letter]
- ? table `has letter {
- ^ table `get letter {[count] count +: 1 }
- } {
- table `set (letter; 1)
- }
- }
- table
-)
-
-`is_main ([]
- `use "sys" [args]
- ? 3 != `length args {
- `return `error String $ (
- "#BOLD#RED#Got: %#\n" `format " " `join args
- "#BOLD#WHITE#Usage: % % <file...>#\n" `format (args `get 0; args `get 1)
- )
- }
- read_log : ([filename]
- `use "io" [open; read]
- `log letter_table `sync read `sync open `sync filename
- )
- @ 2 .. `length args {[arg_index] read_log `async args `get arg_index }
- `value
-)
diff --git a/shared_counter.kpl b/shared_counter.kpl
deleted file mode 100644
index 01f01bd..0000000
--- a/shared_counter.kpl
+++ /dev/null
@@ -1,57 +0,0 @@
-
-// Notice how counter can only be modified as read as [c]
-
-counter : %shared 0
-
-inc_dec : ([I64.amount]
- header : "#BOLD#Thread: %#, #WHITE#Counter:# " `format `thread_id
- body : ^ counter {[c]
- c +: amount
- ? {
- amount = 1 { "#GREEN#%#" `format c }
- amount = -1 { "#RED#%#" `format c }
- { "#UNDERLINE#INVALID#" }
- }
- }
- `log ,(header; body; "\n")
-)
-
-inc : inc_dec `bind 1
-dec : inc_dec `bind -1
-
-Inc_Dec_fn `type dec
-
-actions : %const 1_000
-
-batch : %const 100
-
-runner : ([Inc_Dec_fn.fn]
- total : actions
- @ total {
- `log "#WHITE#Batch: #CYAN#% #MAGENTA#%#\n" `format (fn; total)
- v : Array[`async_type fn] $ ()
- @ 1 .. batch { v `push `async fn }
- @ v {[x] `await x }
- total -: batch
- }
-)
-
-i : runner `async inc
-d : runner `async dec
-
-`log "#BOLD#YELLOW#SLEEPING THREAD %#\n" `format `thread_id
-
-`use "time" [Seconds]
-`use "sys" [sleep]
-
-sleep `sync Seconds $ 1
-
-`await (d; i)
-
-`log ^ counter {[c]
- ? !c {
- `format "#BOLD#GREEN#VALID#\n"
- } {
- `format "#BOLD#RED#INVALID#\n"
- }
-}
diff --git a/sieve.kpl b/sieve.kpl
deleted file mode 100644
index 92fdac9..0000000
--- a/sieve.kpl
+++ /dev/null
@@ -1,78 +0,0 @@
-
-// Asynchronous and Synchronous Sieve of Eratosthenes
-
-sieve_init : ([I64.n]
- ? n > 1 {
- `value +(n; 1) `array ([index] ? index > 1 { `true } { `false })
- } {
- `error "Sieve argument must be a value greater then 1"
- }
-)
-
-sieve_get_primes : ([%ref Array[Bool].table]
- ([result; value; index]
- ? value { result `push I64 $ index }
- result
- ) `reduce (Array[I64] $ (); table)
-)
-
-`export sieve_process : ([I64.n]
- table : %shared sieve_init `sync n
- find : ([I64.prime; I64.n]
- sub_tasks : Array[`async_type find] $ ()
- not_prime : Array[I64] $ ()
- @ +(1; prime) .. n {[check]
- ? check % prime {
- ? ^ table {[t] t `get check } { sub_tasks `push find `aysnc (check; n) }
- } {
- not_prime `push check
- }
- }
- ^ table {[t] @ not_prime {[value] t `set (value; `false) } }
- `await sub_tasks
- )
- find `sync (2; n)
- `value ^ table {[t] sieve_get_primes `sync t }
-)
-
-`export sieve_native : ([I64.n]
- table : sieve_init `sync n
- prime : 2
- @ prime < n {
- @ ! table `get prime { prime +: 1 }
- multiplier : 2
- @ {
- result : prime * multiplier
- multiplier +: 1
- ? result > n { `break }
- table `set (result; `false)
- }
- prime +: 1
- }
- `value sieve_get_primes `sync table
-)
-
-`is_main ([]
- `use "sys" [args]
- ? 4 != `length args {
- `return `error String $ (
- "#BOLD#RED#Got: %#\n" `format " " `join args
- "#BOLD#WHITE#Usage: % % <-sync|-async> <n>#\n" `format (args `get 0; args `get 1)
- )
- }
- n : I64 $ args `get -1
- primes : # args `get -2 {
- "-sync" {
- `log "#BOLD#CYAN#CALLING SYNC#\n"
- sieve_native `sync n
- }
- "-async" {
- `log "#BOLD#CYAN#CALLING ASYNC#\n"
- sieve_process `sync n
- }
- { `return `error "#BOLD#RED#First argument must be -sync or -async#\n" }
- }
- `log primes
- `log "#BOLD#GREEN#COMPLETE#\n"
- `value
-)