Super-d numbers: Difference between revisions

m
→‎{{header|Python}}: Added a functionally composed draft
(→‎{{header|Ruby}}: Added Ruby)
m (→‎{{header|Python}}: Added a functionally composed draft)
Line 791:
 
=={{header|Python}}==
===Procedural===
<lang python>from itertools import islice, count
 
Line 813 ⟶ 814:
7: 140997, 490996, 1184321, 1259609, 1409970, 1783166, 1886654, 1977538, 2457756, 2714763
8: 185423, 641519, 1551728, 1854230, 6415190, 12043464, 12147605, 15517280, 16561735, 18542300</pre>
 
===Functional===
<lang python>'''Super-d numbers'''
 
from itertools import count, islice
from functools import reduce
 
 
# ------------------------ SUPER-D -------------------------
 
# super_d :: Int -> Either String [Int]
def super_d(d):
'''Either a message, if d is out of range, or
an infinite series of super_d numbers for d.
'''
if isinstance(d, int) and 1 < d < 10:
ds = d * str(d)
 
def p(x):
return ds in str(d * x ** d)
return Right(filter(p, count(2)))
else:
return Left(
'Super-d is defined only for integers drawn from {2..9}'
)
 
 
# ------------------------- TESTS --------------------------
# main :: IO ()
def main():
'''Attempted sampling of first 10 values for d <- [1..6],
where d = 1 is out of range.
'''
for v in map(
lambda x: either(
append(str(x) + ' :: ')
)(
compose(
append('First 10 super-' + str(x) + ': '),
showList
)
)(
bindLR(
super_d(x)
)(compose(Right, take(10)))
),
enumFromTo(1)(6)
): print(v)
 
 
# ------------------------ GENERIC -------------------------
 
# Left :: a -> Either a b
def Left(x):
'''Constructor for an empty Either (option type) value
with an associated string.
'''
return {'type': 'Either', 'Right': None, 'Left': x}
 
 
# Right :: b -> Either a b
def Right(x):
'''Constructor for a populated Either (option type) value'''
return {'type': 'Either', 'Left': None, 'Right': x}
 
 
# append (++) :: [a] -> [a] -> [a]
# append (++) :: String -> String -> String
def append(xs):
'''A list or string formed by
the concatenation of two others.
'''
def go(ys):
return xs + ys
return go
 
 
# bindLR (>>=) :: Either a -> (a -> Either b) -> Either b
def bindLR(m):
'''Either monad injection operator.
Two computations sequentially composed,
with any value produced by the first
passed as an argument to the second.
'''
def go(mf):
return (
mf(m.get('Right')) if None is m.get('Left') else m
)
return go
 
 
# compose :: ((a -> a), ...) -> (a -> a)
def compose(*fs):
'''Composition, from right to left,
of a series of functions.
'''
def go(f, g):
def fg(x):
return f(g(x))
return fg
return reduce(go, fs, lambda x: x)
 
 
# either :: (a -> c) -> (b -> c) -> Either a b -> c
def either(fl):
'''The application of fl to e if e is a Left value,
or the application of fr to e if e is a Right value.
'''
return lambda fr: lambda e: fl(e['Left']) if (
None is e['Right']
) else fr(e['Right'])
 
 
# enumFromTo :: Int -> Int -> [Int]
def enumFromTo(m):
'''Enumeration of integer values [m..n]
'''
return lambda n: range(m, 1 + n)
 
 
# showList :: [a] -> String
def showList(xs):
'''Stringification of a list.'''
return '[' + ','.join(str(x) for x in xs) + ']'
 
 
# take :: Int -> [a] -> [a]
# take :: Int -> String -> String
def take(n):
'''The prefix of xs of length n,
or xs itself if n > length xs.
'''
def go(xs):
return islice(xs, n)
return go
 
 
# MAIN ---
if __name__ == '__main__':
main()</lang>
{{Out}}
<pre>1 :: Super-d is defined only for integers drawn from {2..9}
First 10 super-2: [19,31,69,81,105,106,107,119,127,131]
First 10 super-3: [261,462,471,481,558,753,1036,1046,1471,1645]
First 10 super-4: [1168,4972,7423,7752,8431,10267,11317,11487,11549,11680]
First 10 super-5: [4602,5517,7539,12955,14555,20137,20379,26629,32767,35689]
First 10 super-6: [27257,272570,302693,323576,364509,502785,513675,537771,676657,678146]</pre>
 
=={{header|Raku}}==
9,655

edits