Skip to content

Commit

Permalink
style: formatting examples
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperFola committed Sep 19, 2024
1 parent b18d9e6 commit 3ade06a
Show file tree
Hide file tree
Showing 15 changed files with 340 additions and 319 deletions.
22 changes: 13 additions & 9 deletions examples/99bottles.ark
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
# Lyrics from the song:
#
#
# 99 bottles of beer on the wall
# 99 bottles of beer
# Take one down, pass it around
# 98 bottles of beer on the wall
#
#
# 98 bottles of beer on the wall
# 98 bottles of beer
# Take one down, pass it around
# 97 bottles of beer on the wall


(let arg (if (>= (len sys:args) 1) (toNumber (@ sys:args 0)) nil))
(let i (if (nil? arg) 100 arg))
(let arg
(if (>= (len sys:args) 1)
(toNumber (@ sys:args 0))
nil))
(let i
(if (nil? arg)
100
arg))

(mut n i)
(while (> n 1) {
(print (str:format "{} Bottles of beer on the wall\n{} bottles of beer\nTake one down, pass it around" n n))
(set n (- n 1))
(print (str:format "{} Bottles of beer on the wall." n))})
(print (str:format "{} Bottles of beer on the wall\n{} bottles of beer\nTake one down, pass it around" n n))
(set n (- n 1))
(print (str:format "{} Bottles of beer on the wall." n)) })
19 changes: 9 additions & 10 deletions examples/ackermann.ark
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
# Due to its definitions in terms of extremely deep recursion, it can be used as a
# benchmark of a compiler's ability to optimize recursion, which is the reason why
# we are using this function to benchmark the language.

(let ackermann (fun (m n) {
(if (> m 0)
# then
(if (= 0 n)
# then
(ackermann (- m 1) 1)
# else
(ackermann (- m 1) (ackermann m (- n 1))))
# else
(+ 1 n))}))
(if (> m 0)
# then
(if (= 0 n)
# then
(ackermann (- m 1) 1)
# else
(ackermann (- m 1) (ackermann m (- n 1))))
# else
(+ 1 n)) }))

(print "Ackermann-Péter function, m=3, n=6: " (ackermann 3 6))
182 changes: 91 additions & 91 deletions examples/blockchain.ark
Original file line number Diff line number Diff line change
Expand Up @@ -7,88 +7,97 @@

# define what an ArkCoin block is
(let make:block (fun (index timestamp data previous_hash) {
(let hash (hash:sha256 (+ (toString index) (toString (math:floor timestamp)) (json:toString data) previous_hash)))
(print "made block " hash)
(fun (&index &timestamp &data &previous_hash &hash) ())}))
(let hash (hash:sha256 (+ (toString index) (toString (math:floor timestamp)) (json:toString data) previous_hash)))
(print "made block " hash)
(fun (&index &timestamp &data &previous_hash &hash) ()) }))

(let make:block:fromJSON (fun (data)
(make:block (json:get data "index")
(json:get data "timestamp")
(json:get data "data")
(json:get data "hash"))))
(let make:block:fromJSON (fun (data) (make:block (json:get data "index") (json:get data "timestamp") (json:get data "data") (json:get data "hash"))))

# generate genesis block
(let make:genesis_block (fun ()
(make:block 0 (time) (json:fromList [
"type" "Genesis block"
"proof-of-work" 1
]) "deadbeef")))
(let make:genesis_block (fun () (make:block 0 (time) (json:fromList ["type" "Genesis block" "proof-of-work" 1]) "deadbeef")))

# let the user add their miner address if we can't find it
(let miner_address (if (not (io:fileExists? "miner.address"))
(let miner_address
(if (not (io:fileExists? "miner.address"))
(input "miner address> ")
(io:readFile "miner.address")))
(io:writeFile "miner.address" miner_address)

# this node's blockchain copy
(mut blockchain (list (make:genesis_block)))
(mut blockchain [(make:genesis_block)])

# storing the transactions that this node has
(mut nodes_transactions [])

# storing the url data of every other node in the network so we can talk with them
(mut peer_nodes [])

# magic number for the proof of work
(let magic 12)

(let find_new_chains (fun () {
(print "finding new chains")

# get the blockchains of every other node
(mut other_chains [])
(list:forEach peer_nodes (fun (url) {
(let cli (http:client:create url 80))
(let tmp (http:client:get cli "/"))
(if (not (nil? tmp))
{
(let content (make:block:fromJSON (json:fromString (@ tmp 1))))
(set other_chains (append other_chains content))})
(del cli)}))
other_chains }))

(let verify_proof_of_work (fun (proof last_proof)
(and (> proof last_proof) (= 0 (mod proof magic)) (= 0 (mod proof last_proof)))))
(print "finding new chains")

# get the blockchains of every other node
(mut other_chains [])

(list:forEach
peer_nodes
(fun (url) {
(let cli (http:client:create url 80))
(let tmp (http:client:get cli "/"))

(if (not (nil? tmp)) {
(let content (make:block:fromJSON (json:fromString (@ tmp 1))))
(set other_chains (append other_chains content)) })
(del cli) }))
other_chains }))

(let verify_proof_of_work (fun (proof last_proof) (and (> proof last_proof) (= 0 (mod proof magic)) (= 0 (mod proof last_proof)))))

(let verify_chain (fun (chain) {
(print "verifying chain")
(print "verifying chain")

(mut previous nil)
(mut ok? true)
(list:forEach chain (fun (block) {
# no need to continue checking the blocks if a block wasn't ok
(if (and ok? (not (nil? previous)))
(set ok? (verify_proof_of_work (json:get block.data "proof-of-work") (json:get previous.data "proof-of-work"))))
(set previous block)}))
ok? }))
(mut previous nil)
(mut ok? true)

(list:forEach
chain
(fun (block) {
# no need to continue checking the blocks if a block wasn't ok
(if (and ok? (not (nil? previous)))
(set ok? (verify_proof_of_work (json:get block.data "proof-of-work") (json:get previous.data "proof-of-work"))))
(set previous block) }))
ok? }))

(let consensus (fun () {
(print "consensus running")
(print "consensus running")

(let other_chains (find_new_chains))

(let other_chains (find_new_chains))
# if our chain isn't longest, then we store the longest
(list:forEach other_chains (fun (chain) {
(if (and (< (len blockchain) (len chain)) (verify_chain chain))
(set blockchain chain))}))}))
# if our chain isn't longest, then we store the longest
(list:forEach
other_chains
(fun (chain) {
(if (and (< (len blockchain) (len chain)) (verify_chain chain))
(set blockchain chain)) })) }))

(let proof_of_work (fun (last_proof) {
(print "proof of work being generated")
(print "proof of work being generated")

(mut inc (+ 1 last_proof))
# keep incrementing until it's equal to a number divisible by 12 and
# the proof of work of the previous block in the chain
(while (not (and (= 0 (mod inc magic))) (= 0 (mod inc last_proof)))
(set inc (+ 1 inc)))
inc }))
(mut inc (+ 1 last_proof))

# keep incrementing until it's equal to a number divisible by 12 and
# the proof of work of the previous block in the chain
(while (not (and (= 0 (mod inc magic))) (= 0 (mod inc last_proof)))
(set inc (+ 1 inc)))
inc }))

(let srv (http:server:create))
(http:server:post srv "/transaction" (fun (request) {
(http:server:post
srv
"/transaction"
(fun (request) {
(print "posting block " request)

# on each post request, extract transaction data
Expand All @@ -100,69 +109,60 @@
(print (str:format "AMOUNT: {}" (json:get new "amount")))

# return value
[200 "transaction submission successful" "text/plain"]}))
[200 "transaction submission successful" "text/plain"] }))

(http:server:get srv "/blocks" (fun (_) {
(http:server:get
srv
"/blocks"
(fun (_) {
(print "fetching blocks")

(consensus)
(mut to_send [])
(list:forEach blockchain (fun (data) {
(set to_send (append to_send (json:fromList [
"index" data.index
"timestamp" data.timestamp
"data" data.data
"hash" data.hash])))}))

(list:forEach
blockchain
(fun (data) {
(set to_send (append to_send (json:fromList ["index" data.index "timestamp" data.timestamp "data" data.data "hash" data.hash]))) }))

(mut str (toString (@ to_send 0)))
(list:forEach (tail to_send) (fun (e)
(set str (+ str ", " (toString e)))))

[200 (+ "{\"chain\": [" str "]}") "application/json"]}))
(list:forEach
(tail to_send)
(fun (e)
(set str (+ str ", " (toString e)))))
[200 (+ "{\"chain\": [" str "]}") "application/json"] }))

(http:server:get srv "/mine" (fun (data) {
(http:server:get
srv
"/mine"
(fun (data) {
(print "mining block")
(print (type data))
(if (not (nil? data))
(print (http:params:toList data)))

(if (not (nil? data)) (print (http:params:toList data)))
(set data "")

(let last_block (@ blockchain -1))
(let last_proof (json:get last_block.data "proof-of-work"))

# find the proof of work for the current block being mined
# the program will hang here until a new proof of work is found
(let proof (proof_of_work last_proof))

# once we have the proof of work, we can mine a block so we reward the miner by adding a transaction
(set nodes_transactions (append nodes_transactions (json:fromList
[
"from" "network"
"to" miner_address
"amount" 1])))
(set nodes_transactions (append nodes_transactions (json:fromList ["from" "network" "to" miner_address "amount" 1])))
(print "make block")

# gather the data needed to create a new block
(mut new_block (make:block
(+ 1 last_block.index)
(time)
(json:fromList
[
"proof-of-work" proof
"transactions" nodes_transactions
"content" data ])
last_block.hash))
(mut new_block (make:block (+ 1 last_block.index) (time) (json:fromList ["proof-of-work" proof "transactions" nodes_transactions "content" data]) last_block.hash))

(set blockchain (append blockchain new_block))

# empty transactions list
(set nodes_transactions [])

[
200
(+ "{"
"\"index\": " (toString new_block.index) ","
"\"timestamp\": " (toString new_block.timestamp) ","
"\"data\": " (toString new_block.data) ","
"\"hash\": \"" (toString new_block.hash) "\""
"}")
"application/json"]}))
[200 (+ "{" "\"index\": " (toString new_block.index) "," "\"timestamp\": " (toString new_block.timestamp) "," "\"data\": " (toString new_block.data) "," "\"hash\": \"" (toString new_block.hash) "\"" "}") "application/json"] }))

(print "Listening on localhost:80 for miner " miner_address)
(http:server:listen srv "localhost" 80)
31 changes: 18 additions & 13 deletions examples/callbacks.ark
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
# a function which just prints its argument
(let egg (fun (bar) (print bar)))

# the data we're going to give to this function
(let data ["Iron Man" "is" "Tony Stark"])

# a list of function call which should be executed later on
(mut callbacks [])

(print "Data: " data)
(print "Generating callbacks")
(mut acc 0)

# here we are filling the list callbacks
(while (!= acc (len data)) {
(mut d (@ data acc))
# by putting in it closures that captured d, an element of `data`
# and call the function egg on it
(set callbacks (append callbacks (fun (&d) (egg d))))
(set acc (+ 1 acc))})
(mut d (@ data acc))

# by putting in it closures that captured d, an element of `data`
# and call the function egg on it
(set callbacks (append callbacks (fun (&d) (egg d))))
(set acc (+ 1 acc)) })

# then we reset the accumulator
(set acc 0)
(while (!= acc (len callbacks)) {
# we print what was stored in the closure using dot notation
(mut stored (@ callbacks acc))
(print "stored: " stored.d)
# and then we call the closure itself (be careful: (@ callbacks acc) only returns the callback,
# thus we need to put it in another pair of parens to call the callback)
(puts "Calling callback number " acc ": ")
((@ callbacks acc))
(set acc (+ 1 acc))})
# we print what was stored in the closure using dot notation
(mut stored (@ callbacks acc))
(print "stored: " stored.d)

# and then we call the closure itself (be careful: (@ callbacks acc) only returns the callback,
# thus we need to put it in another pair of parens to call the callback)
(puts "Calling callback number " acc ": ")
((@ callbacks acc))
(set acc (+ 1 acc)) })
Loading

0 comments on commit 3ade06a

Please sign in to comment.