CUSIP: Difference between revisions

287 bytes added ,  5 years ago
→‎Python :: Composition of pure functions: Pylinted for Python 3, tidied, added a {works with} tag
(→‎Python :: Composition of pure functions: Pylinted for Python 3, tidied, added a {works with} tag)
Line 1,748:
 
===Composition of pure functions===
{{Works with|Python|3.7}}
Composing a set of pure functions, including a number of general and reusable abstractions:
<lang python>from itertools import (cycle, islice, starmap)'''CUSIP'''
 
from itertools import (cycle, islice)
from functools import (reduce)
from operator import (add)
Line 1,758 ⟶ 1,761:
def isCusip(dct):
'''Test for the validity of a CUSIP string in the
context of a supplied dictionary of charChar values'''.
'''
def go(s):
ns = [dct[c] for c in list(s) if c in dct]
Line 1,766 ⟶ 1,770:
sum(zipWith(
lambda f, x: add(*divmod(f(x), 10))
)(cycle([stetidentity, double]))(
take(8)(ns)
)) % 10
Line 1,796 ⟶ 1,800:
# main :: IO ()
def main():
'''TestsTest for validity as a CUSIP string'''
 
# cusipTest :: String -> Bool
cusipTest = isCusip(cusipCharDict())
 
print(
tabulatedfTable('Validmain.__doc__ as+ CUSIP string':\n')(repr)(str)(
cusipTestisCusip(cusipCharDict())
)([
'037833100',
Line 1,814 ⟶ 1,815:
)
 
# GENERIC -------------------------------------------------
 
# FORMATTING ----------------------------------------------
 
# tabulatedfTable :: String -> (a -> bString) -> [a] -> String
# (b -> String) -> (a -> b) -> [a] -> String
def tabulatedfTable(s):
'''Heading -> x display function -> fx display function ->
f -> xs -> tabular string.
'''
def go(xShow, fxShow, f, xs):
ys = [xShow(x) for x in xs]
w = width(max(xsmap(len, key=widthys))
return s + '\n' + '\n'.join([map(
str(lambda x), y: y.rjust(w, ' ') + ' -> ' + strfxShow(f(x)) for x in xs,
]) xs, ys
def width(x):)
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(f, xs)
xShow, fxShow, f, xs
)
 
 
# GENERIC -------------------------------------------------
 
# double :: Num -> Num
Line 1,852 ⟶ 1,873:
 
 
# stetidentity :: a -> a
def stetidentity(x):
'''The identity function.'''
The usual 'id' is reserved in Python.'''
return x
 
 
# tabulated :: String -> (a -> b) -> [a] -> String
def tabulated(s):
'''heading -> function -> input List -> tabulated output string'''
def go(f, xs):
def width(x):
return len(str(x))
w = width(max(xs, key=width))
return s + '\n' + '\n'.join([
str(x).rjust(w, ' ') + ' -> ' + str(f(x)) for x in xs
])
return lambda f: lambda xs: go(f, xs)
 
 
Line 1,898 ⟶ 1,905:
# zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
def zipWith(f):
'''A list constructed by zipping with a
'''Zipping with a custom (rather than tuple) function'''
custom function, rather than with the
default tuple constructor.
'''
return lambda xs: lambda ys: (
list(starmapmap(f, zip(xs, ys)))
)
 
Line 1,908 ⟶ 1,918:
main()</lang>
{{Out}}
<pre>ValidTest for validity as a CUSIP string:
 
037833100 -> True
17275R102'037833100' -> True
38259P508'17275R102' -> True
594918104'38259P508' -> True
037833100'594918104' -> True
'68389X106' -> False
'68389X105' -> True</pre>
 
=={{header|Racket}}==
9,659

edits