CUSIP: Difference between revisions

Content added Content deleted
(→‎Python :: Composition of pure functions: Pylinted for Python 3, tidied, added a {works with} tag)
Line 1,748: Line 1,748:


===Composition of pure functions===
===Composition of pure functions===
{{Works with|Python|3.7}}
Composing a set of pure functions, including a number of general and reusable abstractions:
Composing a set of pure functions, including a number of general and reusable abstractions:
<lang python>from itertools import (cycle, islice, starmap)
<lang python>'''CUSIP'''

from itertools import (cycle, islice)
from functools import (reduce)
from functools import (reduce)
from operator import (add)
from operator import (add)
Line 1,758: Line 1,761:
def isCusip(dct):
def isCusip(dct):
'''Test for the validity of a CUSIP string in the
'''Test for the validity of a CUSIP string in the
context of a supplied dictionary of char values'''
context of a supplied dictionary of Char values.
'''
def go(s):
def go(s):
ns = [dct[c] for c in list(s) if c in dct]
ns = [dct[c] for c in list(s) if c in dct]
Line 1,766: Line 1,770:
sum(zipWith(
sum(zipWith(
lambda f, x: add(*divmod(f(x), 10))
lambda f, x: add(*divmod(f(x), 10))
)(cycle([stet, double]))(
)(cycle([identity, double]))(
take(8)(ns)
take(8)(ns)
)) % 10
)) % 10
Line 1,796: Line 1,800:
# main :: IO ()
# main :: IO ()
def main():
def main():
'''Tests'''
'''Test for validity as a CUSIP string'''

# cusipTest :: String -> Bool
cusipTest = isCusip(cusipCharDict())


print(
print(
tabulated('Valid as CUSIP string:')(
fTable(main.__doc__ + ':\n')(repr)(str)(
cusipTest
isCusip(cusipCharDict())
)([
)([
'037833100',
'037833100',
Line 1,814: Line 1,815:
)
)


# GENERIC -------------------------------------------------


# FORMATTING ----------------------------------------------

# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(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 = max(map(len, ys))
return s + '\n' + '\n'.join(map(
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
xs, ys
))
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
xShow, fxShow, f, xs
)


# GENERIC -------------------------------------------------


# double :: Num -> Num
# double :: Num -> Num
Line 1,852: Line 1,873:




# stet :: a -> a
# identity :: a -> a
def stet(x):
def identity(x):
'''The identity function.
'''The identity function.'''
The usual 'id' is reserved in Python.'''
return x
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: Line 1,905:
# zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
# zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
def zipWith(f):
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: (
return lambda xs: lambda ys: (
list(starmap(f, zip(xs, ys)))
map(f, xs, ys)
)
)


Line 1,908: Line 1,918:
main()</lang>
main()</lang>
{{Out}}
{{Out}}
<pre>Valid as CUSIP string:
<pre>Test for validity as a CUSIP string:

037833100 -> True
17275R102 -> True
'037833100' -> True
38259P508 -> True
'17275R102' -> True
594918104 -> True
'38259P508' -> True
'594918104' -> True
68389X106 -> False
'68389X106' -> False
68389X105 -> True</pre>
'68389X105' -> True</pre>


=={{header|Racket}}==
=={{header|Racket}}==