Binary digits: Difference between revisions

Content added Content deleted
(→‎Custom functions: Updated output to conform with task description tests.)
m (→‎Python :: Custom functions: Used divmod in lieu of two operators in boolsFromInt)
Line 3,075: Line 3,075:
decomposition of an integer.'''
decomposition of an integer.'''
def go(x):
def go(x):
return Just((x // 2, bool(x % 2))) if x else Nothing()
(q, r) = divmod(x, 2)
return Just((q, bool(r))) if x else Nothing()
return unfoldl(go)(n)
return unfoldl(go)(n)


Line 3,134: Line 3,135:
'''Integer enumeration from m to n.'''
'''Integer enumeration from m to n.'''
return lambda n: list(range(m, 1 + n))
return lambda n: list(range(m, 1 + n))


# fmapTuple (<$>) :: (a -> b) -> (a, a) -> (a, b)
def fmapTuple(f):
'''A pair in which f has been applied to
the second item.'''
return lambda tpl: (tpl[0], f(tpl[1]))