Added lot more example code. (0.39.8).
This commit is contained in:
parent
3b70d6c92a
commit
564999709c
32 changed files with 1353 additions and 53 deletions
37
examples/io/csv_reader.fun
Executable file
37
examples/io/csv_reader.fun
Executable file
|
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-03-25
|
||||
*/
|
||||
|
||||
#include <strings.fun>
|
||||
|
||||
path = "./examples/data/sample.csv"
|
||||
|
||||
// Very small CSV reader (no quotes/escapes) for demo purposes
|
||||
fun parse_csv_line(line)
|
||||
return str_split(str_trim(line), ",")
|
||||
|
||||
data = read_file(path)
|
||||
if (len(data) == 0)
|
||||
print("No CSV content at: " + path)
|
||||
else
|
||||
rows = str_split(data, "\n")
|
||||
for row in rows
|
||||
if (len(str_trim(row)) == 0) continue
|
||||
cols = parse_csv_line(row)
|
||||
print("ROW: " + to_string(cols))
|
||||
|
||||
/* Expected output (from examples/io/sample.csv):
|
||||
ROW: [array n=3]
|
||||
ROW: [array n=3]
|
||||
ROW: [array n=3]
|
||||
ROW: [array n=3]
|
||||
*/
|
||||
25
examples/io/read_write_file.fun
Executable file
25
examples/io/read_write_file.fun
Executable file
|
|
@ -0,0 +1,25 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-03-25
|
||||
*/
|
||||
|
||||
#include <strings.fun>
|
||||
|
||||
path = "./tmp/example.txt"
|
||||
print("Writing to: " + path)
|
||||
write_file(path, "Hello from Fun!\n")
|
||||
txt = read_file(path)
|
||||
print("Read back: " + str_trim(txt))
|
||||
|
||||
/* Expected output:
|
||||
Writing to: ./tmp/example.txt
|
||||
Read back: Hello from Fun!
|
||||
*/
|
||||
412
examples/io/word_count.fun
Executable file
412
examples/io/word_count.fun
Executable file
|
|
@ -0,0 +1,412 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-03-25
|
||||
*/
|
||||
|
||||
// String helpers
|
||||
#include <strings.fun>
|
||||
|
||||
path = "./README.md"
|
||||
txt = read_file(path)
|
||||
if (len(txt) == 0)
|
||||
print("no content: " + path)
|
||||
return 0
|
||||
|
||||
txt = str_to_lower(txt)
|
||||
txt = str_replace_all(txt, "\n", " ")
|
||||
parts = str_split(txt, " ")
|
||||
freq = {}
|
||||
for w in parts
|
||||
w = str_trim(w)
|
||||
if (len(w) == 0) continue
|
||||
c = to_number(freq[w])
|
||||
if (c <= 0) c = 0
|
||||
freq[w] = c + 1
|
||||
|
||||
for k in keys(freq)
|
||||
print(k + ": " + to_string(freq[k]))
|
||||
|
||||
/* Possible output (first 10 lines; counts depend on README.md contents):
|
||||
#: 1
|
||||
fun: 19
|
||||
([https://fun-lang.xyz](https://fun-lang.xyz)): 1
|
||||
##: 9
|
||||
what: 2
|
||||
is: 16
|
||||
fun?: 1
|
||||
a: 9
|
||||
small,: 1
|
||||
strict,: 1
|
||||
and: 15
|
||||
simple: 3
|
||||
programming: 5
|
||||
language: 5
|
||||
that: 4
|
||||
runs: 1
|
||||
on: 3
|
||||
compact: 1
|
||||
stack-based: 1
|
||||
virtual: 1
|
||||
machine.: 1
|
||||
the: 24
|
||||
c: 2
|
||||
core: 3
|
||||
intentionally: 1
|
||||
minimal;: 1
|
||||
most: 6
|
||||
functionality: 1
|
||||
standard: 2
|
||||
libraries: 2
|
||||
are: 6
|
||||
implemented: 2
|
||||
in: 19
|
||||
itself.: 2
|
||||
emphasizes: 1
|
||||
simplicity,: 1
|
||||
consistency,: 1
|
||||
joy: 3
|
||||
coding.: 2
|
||||
an: 1
|
||||
experiment,: 1
|
||||
just: 2
|
||||
for: 6
|
||||
fun,: 3
|
||||
but: 5
|
||||
works!: 1
|
||||
highly: 2
|
||||
strict: 1
|
||||
language,: 1
|
||||
also: 1
|
||||
simple.: 1
|
||||
it: 2
|
||||
looks: 2
|
||||
like: 4
|
||||
python: 1
|
||||
(my: 1
|
||||
favorite: 1
|
||||
language),: 1
|
||||
there: 2
|
||||
differences.: 1
|
||||
influenced: 1
|
||||
by: 2
|
||||
**[bash](https://www.gnu.org/software/bash/)**,: 1
|
||||
**[c](https://en.wikipedia.org/wiki/the_c_programming_language)**,: 1
|
||||
**[lua](https://www.lua.org/)**,: 1
|
||||
php,: 1
|
||||
**[python](https://www.python.org/)**,: 1
|
||||
rust: 1
|
||||
(most: 1
|
||||
influences: 1
|
||||
came: 1
|
||||
from: 2
|
||||
linked: 1
|
||||
languages).: 1
|
||||
will: 4
|
||||
ever: 1
|
||||
be: 5
|
||||
100%: 1
|
||||
free: 1
|
||||
under: 1
|
||||
terms: 1
|
||||
of: 3
|
||||
[apache-2.0: 1
|
||||
license](https://opensource.org/license/apache-2-0).: 1
|
||||
idea: 1
|
||||
-: 48
|
||||
simplicity: 2
|
||||
consistency: 2
|
||||
to: 7
|
||||
extend: 1
|
||||
hackable: 1
|
||||
coding: 2
|
||||
fun!: 1
|
||||
characteristics: 1
|
||||
dynamic: 1
|
||||
optionally: 1
|
||||
statically: 1
|
||||
typed: 1
|
||||
type: 1
|
||||
safety: 1
|
||||
written: 6
|
||||
(c99): 1
|
||||
internal: 1
|
||||
libs: 2
|
||||
with: 2
|
||||
no_camel_case: 1
|
||||
even: 1
|
||||
when: 1
|
||||
except: 1
|
||||
class: 1
|
||||
names: 1
|
||||
only: 2
|
||||
minimal: 1
|
||||
function: 1
|
||||
set: 1
|
||||
c,: 1
|
||||
other: 1
|
||||
functions: 1
|
||||
manifesto: 1
|
||||
built: 2
|
||||
idea:: 1
|
||||
should: 6
|
||||
enjoyable,: 1
|
||||
elegant,: 1
|
||||
consistent.: 1
|
||||
###: 5
|
||||
philosophy: 1
|
||||
**fun: 2
|
||||
fun**<br>: 1
|
||||
spark: 1
|
||||
creativity,: 1
|
||||
not: 4
|
||||
frustration.: 1
|
||||
code: 3
|
||||
feels: 3
|
||||
light,: 1
|
||||
playful,: 1
|
||||
rewarding.: 1
|
||||
uses: 1
|
||||
nothing**<br>: 1
|
||||
minimalism: 1
|
||||
power.: 1
|
||||
no: 7
|
||||
unnecessary: 1
|
||||
features,: 1
|
||||
endless: 1
|
||||
syntax: 1
|
||||
variations,: 1
|
||||
formatting: 1
|
||||
debates.: 1
|
||||
clean,: 1
|
||||
uniform: 1
|
||||
code.: 1
|
||||
**indentation: 1
|
||||
truth**<br>: 1
|
||||
two: 1
|
||||
spaces,: 1
|
||||
always.: 1
|
||||
tabs,: 1
|
||||
four-space: 1
|
||||
wars.: 1
|
||||
look: 1
|
||||
same: 2
|
||||
everywhere,: 1
|
||||
your: 1
|
||||
laptop: 1
|
||||
/usr/bin/fun.: 1
|
||||
**one: 1
|
||||
way: 1
|
||||
do: 1
|
||||
it**<br>: 1
|
||||
clutter,: 1
|
||||
15: 1
|
||||
ways: 1
|
||||
writing: 1
|
||||
thing.: 1
|
||||
means: 1
|
||||
clarity.: 1
|
||||
**hackable: 1
|
||||
nature**<br>: 1
|
||||
small: 1
|
||||
embeddable,: 1
|
||||
lua.: 1
|
||||
easy: 1
|
||||
understand,: 1
|
||||
extend,: 1
|
||||
tinker: 1
|
||||
—: 2
|
||||
true: 1
|
||||
hacker: 1
|
||||
spirit.: 1
|
||||
**beautiful: 1
|
||||
defaults**<br>: 1
|
||||
doesn’t: 1
|
||||
need: 1
|
||||
linters,: 1
|
||||
formatters,: 1
|
||||
or: 3
|
||||
style: 1
|
||||
guides.: 1
|
||||
beauty: 1
|
||||
in.: 1
|
||||
community: 3
|
||||
about: 2
|
||||
being: 1
|
||||
fastest: 1
|
||||
feature-rich.: 1
|
||||
it’s: 1
|
||||
sharing: 1
|
||||
be:: 1
|
||||
respectful: 1
|
||||
curious: 1
|
||||
creative: 1
|
||||
open: 1
|
||||
everyone: 1
|
||||
name: 1
|
||||
says:: 1
|
||||
unites: 1
|
||||
nerds.: 1
|
||||
please: 1
|
||||
visit: 1
|
||||
[fun: 1
|
||||
page](https://fun-lang.xyz/community/): 1
|
||||
get: 1
|
||||
touch.: 1
|
||||
goal: 1
|
||||
home: 1
|
||||
developers: 1
|
||||
who:: 1
|
||||
love: 1
|
||||
minimal,: 1
|
||||
elegant: 1
|
||||
tools: 1
|
||||
believe: 1
|
||||
freedom: 1
|
||||
want: 1
|
||||
write: 1
|
||||
good: 2
|
||||
may: 2
|
||||
change: 1
|
||||
world: 1
|
||||
make: 1
|
||||
little: 1
|
||||
more: 1
|
||||
fun.: 1
|
||||
features: 2
|
||||
functions/classes/objects: 1
|
||||
if/else: 2
|
||||
try/catch/finally: 1
|
||||
lib: 1
|
||||
(./lib/): 1
|
||||
see: 2
|
||||
[./lib/](https://git.xw3.org/fun/fun/src/branch/main/lib): 2
|
||||
library: 1
|
||||
provides.: 1
|
||||
optional: 1
|
||||
extensions: 1
|
||||
(build-time: 1
|
||||
selectable: 1
|
||||
/: 2
|
||||
testing: 1
|
||||
this: 1
|
||||
linux: 1
|
||||
actually):: 1
|
||||
[cgi](https://en.wikipedia.org/wiki/common_gateway_interface): 1
|
||||
support: 1
|
||||
builtin: 1
|
||||
using: 1
|
||||
[kcgi](https://kristaps.bsd.lv/kcgi/): 1
|
||||
(optional): 11
|
||||
☐: 2
|
||||
[curl: 1
|
||||
(libcurl)](./docs/external/curl.md): 1
|
||||
☑: 11
|
||||
[ini: 1
|
||||
(iniparser)](./docs/external/ini.md): 1
|
||||
[json: 1
|
||||
(json-c)](./docs/external/json.md): 1
|
||||
[libsql](./docs/external/libsql.md): 1
|
||||
[pcre2](./docs/external/pcre2.md): 1
|
||||
[pcsc: 1
|
||||
(smart: 1
|
||||
cards)](./docs/external/pcsc.md): 1
|
||||
[openssl](./docs/external/openssl.md): 1
|
||||
[sqlite](./docs/external/sqlite.md): 1
|
||||
[tk: 1
|
||||
(tcl/tk: 1
|
||||
gui)](./docs/external/tcltk.md): 1
|
||||
[xml: 1
|
||||
(libxml2)](./docs/external/xml2.md): 1
|
||||
=: 2
|
||||
done: 1
|
||||
planned: 1
|
||||
progress.: 1
|
||||
note:: 2
|
||||
all: 1
|
||||
above: 1
|
||||
implemented.: 1
|
||||
those: 1
|
||||
who: 1
|
||||
marked: 1
|
||||
"done": 1
|
||||
probaly: 1
|
||||
remain: 1
|
||||
i: 1
|
||||
don't: 1
|
||||
know: 1
|
||||
actually...: 1
|
||||
;): 1
|
||||
some: 2
|
||||
available: 1
|
||||
diretory.: 1
|
||||
future: 1
|
||||
enhancements: 1
|
||||
openssl: 1
|
||||
quickstart: 1
|
||||
(md5): 1
|
||||
dedicated: 1
|
||||
page:: 1
|
||||
./docs/external/openssl.md: 1
|
||||
documentation: 2
|
||||
looking: 1
|
||||
docs?: 1
|
||||
start: 1
|
||||
here:: 1
|
||||
local: 1
|
||||
index:: 1
|
||||
[docs/readme.md](./docs/readme.md): 1
|
||||
handbook:: 1
|
||||
[docs/handbook.md](./docs/handbook.md): 1
|
||||
types: 1
|
||||
overview:: 1
|
||||
[docs/types.md](./docs/types.md): 1
|
||||
repl: 1
|
||||
guide:: 1
|
||||
[docs/repl.md](./docs/repl.md): 1
|
||||
testing:: 1
|
||||
[docs/testing.md](./docs/testing.md): 1
|
||||
troubleshooting:: 1
|
||||
[docs/troubleshooting.md](./docs/troubleshooting.md): 1
|
||||
additional: 1
|
||||
references:: 1
|
||||
specification:: 1
|
||||
[spec/v0.3.md](./spec/v0.3.md): 1
|
||||
(work: 1
|
||||
progress): 1
|
||||
examples: 1
|
||||
demonstrating: 1
|
||||
features:: 1
|
||||
[examples/](./examples/): 1
|
||||
internals: 1
|
||||
vm: 1
|
||||
opcodes: 1
|
||||
live: 1
|
||||
[src/](./src/): 1
|
||||
(see: 1
|
||||
[src/vm/](./src/vm): 1
|
||||
opcode: 1
|
||||
implementations): 1
|
||||
project: 1
|
||||
evolving;: 1
|
||||
documents: 1
|
||||
lag: 1
|
||||
behind.: 1
|
||||
docs: 1
|
||||
index: 1
|
||||
`./docs/readme.md`: 1
|
||||
up‑to‑date: 1
|
||||
entry: 1
|
||||
point.: 1
|
||||
author: 1
|
||||
johannes: 1
|
||||
findeisen: 1
|
||||
<you@hanez.org>: 1
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue