Towers of Hanoi from https://youtu.be/DCKOcY-k2Es
- add
unix.cma
to inline using the Unix library when calling theHanoi.ml
program. You could have included it in a dune file.
ocaml unix.cma hanoi.ml
module Any = { | |
// borrowed from https://github.com/cca-io/rescript-material-ui/blob/master/public/rescript-material-ui/src/Any.res | |
type t; | |
external make: 'a => t = "%identity"; | |
external fromString: string => t = "%identity"; | |
external fromInt: int => t = "%identity"; | |
external unsafeToString: 'a => string = "%identity"; | |
external unsafeToInt: 'a => int = "%identity"; | |
external unsafeGetValue: t => 'a = "%identity"; |
unix.cma
to inline using the Unix library when calling the Hanoi.ml
program. You could have included it in a dune file.ocaml unix.cma hanoi.ml
#!/bin/bash | |
echo "Enter the target directory " | |
read -r target_dir | |
cd "$target_dir" || exit | |
echo "Enter the file extension to search without a dot" | |
read -r old_ext | |
echo "Enter the new file extension to rename to without a dot" |
This is a simple installation and configuration manual to setup a private mailserver. The instructions will be continued in future...
CREATE TABLE IF NOT EXISTS "users" ( | |
"id" SERIAL, | |
"username" VARCHAR(255) UNIQUE, | |
"email" VARCHAR(255) UNIQUE, | |
"password" VARCHAR(255), | |
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, | |
"updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, | |
UNIQUE ("username"), | |
UNIQUE ("email"), | |
PRIMARY KEY ("id") |
let a = Some(10); | |
let b = Some(3); | |
Js.log2("using ocaml 4.12 compiled reasonml let.opt in Rescript", Opts.z(a,b)); |
This was a great example. I wanted to take some time to demonstrate an idiomatic, immutable-style list-processing approach to solving this. In FP, the most powerful general-purpose immutable list-processing functions are the fold functions. They iterate through a list one item at a time, keeping track of a 'current value' which can be anything, and apply some folding function to that current value and each list item to come up with the next 'current value'. There are different types of folding functions which work in slightly different ways, you can learn about them in functional programming books or courses.
Long story short, the appropriate fold function for your list is List.fold_left, which walks through each item of your list from left to right, applying the given function. A simple example: let result = List.fold_left (/) 36 [1, 2, 3]. Now, result = 6, which is 36 / 1 / 2 / 3.
Here's the solution to your example using List.fold_left:
type data_item = {
symbol: string,
next: bool,
BuckleScript bindings to node-glob.
Status: Very basic, but functional
Glob.glob("**/*.js", (_, files) => Array.iter(Js.log, files));
#!/bin/sh | |
# cd .. | |
files=$(find src -name "*.re" && find src -name "*.rei") | |
echo $files | |
for file in $files | |
do | |
echo $PWD/$file | |
# fastreplacestring $file "ReasonReact.NoUpdate" "NoUpdate" | |
fastreplacestring $file "ReasonReact.Update" "Update" |
let sum = n => { | |
let rec loop = (n, k) => | |
if (n == 0) { | |
k(0); | |
} else { | |
([@tailcall] loop)(n - 1, x => k(x + n)); | |
}; | |
([@tailcall] loop)(n, x => x); | |
}; |