Move 'iterable' module

This commit is contained in:
Bartłomiej Pluta
2019-07-09 22:16:10 +02:00
parent 82bc9997c8
commit e3f9cb6551
10 changed files with 14 additions and 6 deletions

View File

@@ -0,0 +1,17 @@
from functools import reduce
from smnp.library.model import Function
from smnp.library.signature import varargSignature, ofTypes
from smnp.type.model import Type
_signature = varargSignature(ofTypes(Type.LIST))
def _function(env, vararg):
if len(vararg) == 1:
return vararg[0]
combined = reduce(lambda x, y: x.value + y.value, vararg)
return Type.list(combined)
function = Function(_signature, _function, 'combine')

View File

@@ -0,0 +1,23 @@
from smnp.library.model import Function
from smnp.library.signature import varargSignature, allTypes
from smnp.type.model import Type
_signature = varargSignature(allTypes())
def _function(env, vararg):
return Type.list(doFlat(vararg, [])).decompose()
def doFlat(input, output=None):
if output is None:
output = []
for item in input:
if item.type == Type.LIST:
doFlat(item.value, output)
else:
output.append(item)
return output
function = Function(_signature, _function, 'flat')

View File

@@ -0,0 +1,27 @@
from smnp.error.runtime import RuntimeException
from smnp.library.model import CombinedFunction, Function
from smnp.library.signature import signature, ofType, ofTypes
from smnp.type.model import Type
_signature1 = signature(ofType(Type.LIST), ofType(Type.INTEGER))
def _function1(env, list, index):
try:
return list.value[index.value]
except KeyError:
raise RuntimeException(f"Attempt to access item which is outside the list", None)
_signature2 = signature(ofType(Type.MAP), ofTypes(Type.INTEGER, Type.STRING, Type.NOTE))
def _function2(env, map, key):
try:
return map.value[key]
except KeyError:
raise RuntimeException(f"Attempt to access unknown key in map", None)
function = CombinedFunction(
'get',
Function(_signature1, _function1),
Function(_signature2, _function2)
)

View File

@@ -0,0 +1,30 @@
from smnp.library.model import Function, CombinedFunction
from smnp.library.signature import varargSignature, listMatches, ofTypes, allTypes, signature, listOfMatchers
from smnp.type.model import Type
_signature1 = varargSignature(listMatches(ofTypes(Type.INTEGER, Type.STRING, Type.NOTE), allTypes()))
def _function1(env, vararg):
map = {}
for entry in vararg:
key, value = entry.value
map[key] = value
return Type.map(map)
_signature2 = signature(listOfMatchers(listMatches(ofTypes(Type.INTEGER, Type.STRING, Type.NOTE), allTypes())))
def _function2(env, list):
map = {}
for entry in list.value:
key, value = entry.value
map[key] = value
return Type.map(map)
function = CombinedFunction(
'Map',
Function(_signature1, _function1),
Function(_signature2, _function2)
)

View File

@@ -0,0 +1,36 @@
from smnp.library.model import CombinedFunction, Function
from smnp.library.signature import signature, ofType
from smnp.note.model import Note
from smnp.type.model import Type
_signature1 = signature(ofType(Type.INTEGER))
def _function1(env, upper):
return Type.list(list(range(upper.value + 1)))
_signature2 = signature(ofType(Type.INTEGER), ofType(Type.INTEGER))
def _function2(env, lower, upper):
return Type.list(list(range(lower.value, upper.value + 1)))
_signature3 = signature(ofType(Type.INTEGER), ofType(Type.INTEGER), ofType(Type.INTEGER))
def _function3(env, lower, upper, step):
return Type.list(list(range(lower.value, upper.value + 1, step.value)))
_signature4 = signature(ofType(Type.NOTE), ofType(Type.NOTE))
def _function4(env, lower, upper):
return Type.list([Type.note(n) for n in Note.range(lower.value, upper.value)])
# TODO
# signature5 = range(note lower, note upper, integer step) OR step = "diatonic" | "chromatic" | "augmented" | "diminish"
function = CombinedFunction(
'range',
Function(_signature1, _function1),
Function(_signature2, _function2),
Function(_signature3, _function3),
Function(_signature4, _function4),
)