Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ mochi.wpu
# Temp
tmp/
temp/
doc/code/*.py
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Mochi
====

Mochi is a dynamically typed programming language for functional programming and actor-style programming.

Its interpreter is written in Python3. The interpreter translates a program written in Mochi to Python3's AST / bytecode.
Expand All @@ -16,6 +17,9 @@ Its interpreter is written in Python3. The interpreter translates a program writ
- Macro, like the traditional macro of Lisp
- Builtin functions includes functions exported by itertools module, recipes, functools module and operator module

## Mailing List

https://mail.python.org/mailman/listinfo/mochi

## Examples
### Factorial
Expand All @@ -36,7 +40,7 @@ def factorial:
n: factorial(n, 1)
0, acc: acc
n, acc: factorial(n - 1, acc * n)

factorial(10000)
# => 28462596809170545189064132121198688...
```
Expand Down Expand Up @@ -415,7 +419,7 @@ match nums([1, 2, 3]):

Positive = predicate(-> $1 > 0)
Even = predicate(-> $1 % 2 == 0)
EvenAndPositive = predicate(-> ($1 % 2 == 0) and ($1 >= 0))
EvenAndPositive = predicate(-> ($1 % 2 == 0) and ($1 >= 0))

match 10:
EvenAndPositive n: str(n) + ':Even and Positive'
Expand Down Expand Up @@ -560,7 +564,7 @@ show(1.0, 'msg')
FileMode = options('r', 'w', 'a', 'r+', 'w+', 'a+')

def open_file:
str path, FileMode mode:
str path, FileMode mode:
open(path, mode)
str path:
open(path, 'r')
Expand Down Expand Up @@ -593,7 +597,7 @@ pvector(map(-> $1 * 2, [1, 2, 3]))
# => pvector([2, 4, 6])
```

### Pipeline operator
### Pipeline operator
```python
add = -> $1 + $2
2 |> add(10) |> add(12)
Expand Down Expand Up @@ -679,7 +683,7 @@ foreach([1, 2, 3]) @ (item) ->
```python
def foo(a, b, c):
a + b + c

a = 1
b = 2
c = 3
Expand Down Expand Up @@ -738,10 +742,10 @@ x
```python
module Math:
export add, sub

def add(x, y):
x + y

def sub(x, y):
x - y

Expand Down
24 changes: 24 additions & 0 deletions doc/code/average.mochi
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# file: average.mochi

def raise_missing_args(missing):
"""Raise a TypeError showing the number of missing arguments.
"""
msg = 'missing {} required argument{}'.format(
missing, 's' if missing > 1 else '')
raise TypeError(msg)

def avg:
# Calculate the average of two or more numbers.
x, y, &z: (x + y + sum(z)) / (2 + len(z))
&args: raise_missing_args(2 - len(args))

print('2, 3', avg(2, 3))
print('2, 3, 7', avg(2, 3, 7))
try:
print('2', avg(2))
except TypeError as err:
print(err)
try:
print('empty', avg())
except TypeError as err:
print(err)
9 changes: 9 additions & 0 deletions doc/code/duplicate_pattern.mochi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# file duplicate_pattern.mochi

def duplicate_pattern:
x, y: 'match 1'
a, b: 'match 2, never being reached'

print(duplicate_pattern(1, 2))
print(duplicate_pattern(10, 20))
print(duplicate_pattern(100, 200))
13 changes: 13 additions & 0 deletions doc/code/func_haskell_style.mochi
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# file func_haskell_style.mochi

def show_num_args:
x: 'one arg: {}'.format(x)
x, y: 'two args: {}, {}'.format(x, y)
x, y, z: 'three args: {}, {}, {}'.format(x, y, z)
&n: '{} args'.format(len(n)) if n else 'no args'

print(show_num_args())
print(show_num_args(10))
print(show_num_args(10, 20))
print(show_num_args(10, 20, 30))
print(show_num_args(1, 2, 3, 4, 5))
18 changes: 18 additions & 0 deletions doc/code/func_py_style.mochi
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# file func_py_style.mochi

def truncate(value, limit):
if value < 0:
0
elif value > limit:
limit
else:
value

print('limit: 10')
print('value: 7', 'result:', truncate(7, 10))
print('value: -7', 'result:',truncate(-7, 10))
print('value: -17', 'result:',truncate(17, 10))

# vals = [7, -7, 17]
# show = -> print('value: {:-4d} result: {:4d}'.format($1, truncate($1, 10)))
# vals |> map(show) |> pvector()
12 changes: 12 additions & 0 deletions doc/code/match_number.mochi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# file: match_number.mochi

from numbers import Number

def match_number:
Number x: 'number'
&x: 'unknown'


print('1', match_number(1))
print('1.5', match_number(1.5))
print('a', match_number('a'))
13 changes: 13 additions & 0 deletions doc/code/match_types.mochi
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# file: match_types.mochi

def match_types:
int x, str y: 'int str'
float x, int y: 'float int'
str x, float y: 'str float'
PVector pv: 'PVector'
&z: 'unknown'

print(10, 'abc', match_types(10, 'abc'))
print(10.5, 3, match_types(10.5, 3))
print('a', 'b', match_types('a', 'b'))
print([1, 2, 3], match_types([1, 2, 3]))
6 changes: 6 additions & 0 deletions doc/code/mymod.mochi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# file mymod.mochi

def add(a, b):
a + b

print(add(2, 3))
8 changes: 8 additions & 0 deletions doc/code/recursive_sum.mochi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# file: recursive_sum.mochi

def sum_:
[head, &tail]: head + sum_(tail)
[]: 0

print(sum_([10, 20, 30.5]))
print(sum_([]))
31 changes: 31 additions & 0 deletions doc/code/recursive_sum_tail.mochi
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# file: recursive_sum_tail.mochi

def sum_:
[head, &tail]: sum_(head, tail)
acc, [head, &tail]: sum_(head + acc, tail)
acc, []: acc
[]: 0


print(sum_([10, 20, 30.5]))
print(sum_([]))
print(sum(range(10000)))

from timeit import default_timer
dft = default_timer

numbers = range(int('1' + '0' * 6))

start = dft()
f_sum = sum_(numbers)
f_time = dft() - start

start = dft()
b_sum = sum(numbers)
b_time = dft() - start

assert f_sum == b_sum

print(f_time)
print(b_time)
print(f_time / b_time)
15 changes: 15 additions & 0 deletions doc/code/unreachable_patterns.mochi
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# file unreachable_patterns.mochi

def show_num_args:
&n: '{} args'.format(len(n)) if n else 'no args'
# all following patterns will never be reached
x: 'one arg: {}'.format(x)
x, y: 'two args: {}, {}'.format(x, y)
x, y, z: 'three args: {}, {}, {}'.format(x, y, z)


print(show_num_args())
print(show_num_args(10))
print(show_num_args(10, 20))
print(show_num_args(10, 20, 30))
print(show_num_args(1, 2, 3, 4, 5))
Loading