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> |
<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 |
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([ |
)(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(): |
||
''' |
'''Test for validity as a CUSIP string''' |
||
# cusipTest :: String -> Bool |
|||
cusipTest = isCusip(cusipCharDict()) |
|||
print( |
print( |
||
fTable(main.__doc__ + ':\n')(repr)(str)( |
|||
isCusip(cusipCharDict()) |
|||
)([ |
)([ |
||
'037833100', |
'037833100', |
||
Line 1,814: | Line 1,815: | ||
) |
) |
||
⚫ | |||
# FORMATTING ---------------------------------------------- |
|||
⚫ | |||
# (b -> String) -> (a -> b) -> [a] -> String |
|||
⚫ | |||
'''Heading -> x display function -> fx display function -> |
|||
f -> xs -> tabular string. |
|||
''' |
|||
⚫ | |||
ys = [xShow(x) for x in xs] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
xShow, fxShow, f, xs |
|||
) |
|||
⚫ | |||
# double :: Num -> Num |
# double :: Num -> Num |
||
Line 1,852: | Line 1,873: | ||
# |
# identity :: a -> a |
||
def |
def identity(x): |
||
'''The identity function. |
'''The identity function.''' |
||
The usual 'id' is reserved in Python.''' |
|||
return x |
return x |
||
⚫ | |||
⚫ | |||
'''heading -> function -> input List -> tabulated output string''' |
|||
⚫ | |||
⚫ | |||
return len(str(x)) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
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: ( |
||
map(f, xs, ys) |
|||
) |
) |
||
Line 1,908: | Line 1,918: | ||
main()</lang> |
main()</lang> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre>Test for validity as a CUSIP string: |
||
⚫ | |||
'037833100' -> True |
|||
'17275R102' -> True |
|||
'38259P508' -> True |
|||
⚫ | |||
68389X106 -> False |
'68389X106' -> False |
||
68389X105 -> True</pre> |
'68389X105' -> True</pre> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |