Skip to main content

A small clone of ramda

Project description

python_ramda

This is a repo try to copy https://github.com/ramda/ramda in python.

install

For whom wants to use this package.

> pip install python-ramda
> pip install python-ramda -U # get the latest

Usage

>>> from ramda import curry
>>> def sum(a, b, c): return a + b + c
>>> curry(sum)(1)(2, 3)
6
>>> import ramda as R # similar to ramda syntax
>>> def sum(a, b, c): return a + b + c
>>> R.curry(sum)(1)(2, 3)
6

Doc

Because the usage of python_ramda is almostly same to ramda, so we don't create any extra doc.

If you feel any behaviour is different from what is should be in ramda, please check below CheckList for more details.

Contribute

For whom wants to contribute to this repo.

# see: https://pre-commit.com/ for more details
$ pre-commit install # please install hooks first

Checkout new branch from main branch directly and create PR.

CheckList

Functions supported now.

  • __
  • add
  # different from ramda, ramda treat null as 0
  >>> R.add(None, None) # float('nan)
  • addIndex
  • adjust
  • all
    • Transducer part is not fully tested.
  • allPass
  • always
  • And (and is a keyword in python)
  • andThen
  • any
  • anyPass
  • ap
  • aperture
  • append
  • apply
  • applySpec
  • applyTo
  • ascend
  • assoc
  • assocPath
  • binary
  • bind
  • both
  • call
  • chain
  • clamp
  • clone

we are simply using python copy module So with no specific reason, we suggest you to use python origin copy module as your first choice.

class Obj:
  def __init__(self, x):
    self.value = x
obj = Obj(42)
clone = R.clone(obj)
obj == clone # False, obj and clone have different references
isinstance(clone, Obj) # True

class Obj:
  def __init__(self, x):
    self.value = x

  def __eq__(self, other):
    return self.value == other.value
obj = Obj(42)
clone = R.clone(obj)
obj == clone # True, if Obj override __eq__ function
isinstance(clone, Obj) # True
  • collectBy
  • comparator
  • complement
  • compose
  • composeWith
  • concat
  • cond
  • construct
  • constructN
  • converge
  • count
  • countBy
  • curry
  • curryN
  • dec
  • defaultTo
  • descend
  • difference
  • differenceWith
  • dissoc
  • dissocPath
  • divide
  • drop
  • dropLast
  • dropLastWhile
  • dropRepeats
  • dropRepeatsWith
  • dropWhile
  • either
  • empty
# We don't support empty object in python
class Obj:
  def __init__(self, value):
    self.value = value
o = Obj(42)
o == R.empty(o) # True, we will return the original cloned object

What we support for now:

  1. dict()
  2. set()
  3. list()
  4. str()
  5. any instance with empty() method
  6. any instance with 'fantasy-land/empty' property
  • endsWith
  • eqBy
  • eqProps
# works for both dict and object
class Obj:
  def __init__(self, v):
    self.v = v
obj1 = Obj(1)
obj2 = Obj(1)
R.eqProps('v', obj1, obj2) # True
R.eqProps('v', {'v': 1}, {'v': 1}) # True
  • equals
R.equals(float('nan'), float('nan')) # True
  • evolve
  • F
  • filter
  • find
  • findIndex
  • findLast
  • findLastIndex
  • flatten
  • flip
  • forEach
  • forEachObjIndexed
  • fromPairs
  • groupBy
  • groupWith
  • gt
  • gte
  • has
  • hasIn
  • hasPath
  • head
  • identical
  • identity
  • ifElse
  • inc
  • includes
  • indexBy
  • indexOf
  • init
  • innerJoin
  • insert
  • insertAll
  • intersection
  • intersperse
  • into
  • invert
  • invertObj
  • invoker
  • is
  • isEmpty
class Obj:
  pass
# Any custom object will be treated as non-empty
R.isEmpty(Obj()) # False
R.isEmpty(None) # False
  • isNil
  • join
  • juxt
  • keys
# When using R.keys(obj) and obj is a class instance, we use obj.__dict__ as keys.
class A:
  c = 'not included'
  def __init__(self):
    self.a = 1
    self.b = 2
a = A()
R.keys(a) # ['a', 'b']
  • keysIn
  • last
  • lastIndexOf
  • length
  • lens
  • lensIndex
  • lensPath
  • lensProp
  • lift
  • liftN
  • lt
  • lte
  • map
  • mapAccum
  • mapAccumRight
  • mapObjIndexed
  • match
  • mathMod
  • Max (max is a keyword in python)

If R.Max(a, b) a and b are with different types, we will compare with str(a) and str(b).

R.Max('A', None) # None, 'A' < 'None'
  • maxBy
  • mean
  • median
  • memoizeWith
  • mergeAll
  • mergeDeepLeft
  • mergeDeepRight
  • mergeDeepWith
  • mergeDeepWithKey
  • mergeLeft
  • mergeRight
  • mergeWith
  • mergeWithKey
  • Min (min is a keyword in python)

If R.Min(a, b) a and b are with different types, we will compare with str(a) and str(b).

R.Min('A', None) # 'A', 'A' < 'None'
  • minBy
  • modify
  • modifyPath
  • modulo
  • move
  • multiply
  • nAry
  • negate
  • none
  • not
  • nth
  • nthArg
  • o
  • objOf
  • of
  • omit

we support both dict type and object type.

class Obj:
  def __init__(self, v1, v2):
    self.v1 = v1
    self.v2 = v2
obj = Obj(1, 2)
R.omit(['v1'], obj) # {'v2': 2}
R.omit(['v1', 'v3'], obj) # {'v2': 2}
  • on
  • once
  • or
  • otherwise
  • over
  • pair
  • partial
  • partialObject
  • partialRight
  • partition
  • path
  • pathEq
  • pathOr
  • paths
  • pathSatisfies
  • pick
  • pickAll

both pick and pickAll support both dict and object type.

class Obj:
  def __init__(self, v1, v2):
    self.v1 = v1
    self.v2 = v2
obj = Obj(1, 2)
R.pick(['v1'], obj) # {'v1': 1}
R.pickAll(['v1', 'v3'], obj) # {'v1': 1, 'v3': None}
  • pickBy
  • pipe
  • pipeWith
  • pluck
# works for both dict and object
class Obj:
  def __init__(self, v1, v2):
    self.v1 = v1
    self.v2 = v2
obj1 = Obj(1, 2)
obj2 = Obj(3, 4)
R.pluck('v1', [obj1, obj2]) # [1, 3]
  • prepend
  • product
  • project
# works for both dict and object
class Obj:
  def __init__(self, v1, v2):
    self.v1 = v1
    self.v2 = v2
obj1 = Obj(1, 2)
obj2 = Obj(3, 4)
R.project(['v1'], [obj1, obj2]) # [{'v1': 1}, {'v1': 3}]
  • promap
  • prop
  • propEq
  • propIs
  • propOr
  • props
  • propSatisfies
  • range
  • reduce
  • reduceBy
  • reduced
  • reduceRight
  • reduceWhile
  • reject
  • remove
  • repeat
  • replace
  • reverse
  • scan
  • sequence
  • set
  • slice
R.slice(1, 3, ['a', 'b', 'c', 'd']) # ['b', 'c']
R.slice(1, None, ['a', 'b', 'c', 'd']) # ['b', 'c', 'd']
  • sort
  • sortBy
  • sortWith
  • split
  • splitAt
  • splitEvery
  • splitWhen
  • splitWhenever
  • startsWith
  • subtract
  • sum
  • symmetricDifference
  • symmetricDifferenceWith
  • T
  • tail
  • take
  • takeLast
  • takeLastWhile
  • takeWhile
  • tap
  • test
  • thunkify
  • times
  • toLower
  • toPairs
  • toPairsIn
  • toString

Partially supported

  • String type, supported

  • for others, just use str(x) instead

  • toUpper

  • transduce

  • transpose

  • traverse

  • trim

  • tryCatch

  • type

  • unapply

  • unary

  • uncurryN

  • unfold

  • union

  • unionWith

  • uniq

  • uniqBy

  • uniqWith

  • unless

  • unnest

  • until

  • unwind

  • update

  • useWith

  • values

  • valuesIn

  • view

  • when

  • where

  • whereAny

  • whereEq

  • without

  • xor

  • xprod

  • zip

  • zipObj

  • zipWith

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_ramda-0.0.12.tar.gz (46.0 kB view details)

Uploaded Source

Built Distribution

python_ramda-0.0.12-py3-none-any.whl (96.3 kB view details)

Uploaded Python 3

File details

Details for the file python_ramda-0.0.12.tar.gz.

File metadata

  • Download URL: python_ramda-0.0.12.tar.gz
  • Upload date:
  • Size: 46.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for python_ramda-0.0.12.tar.gz
Algorithm Hash digest
SHA256 e42ce229cff93fba191d633fe6ea66cb1fd13f5c5685883a4de22feedb8ebc3e
MD5 640e5cee590152f52d9c4025ea95c1cd
BLAKE2b-256 d1b4d11af87d31f09d568d0a319c150ce37b1cc7da5583a4b4e40994ca19f725

See more details on using hashes here.

File details

Details for the file python_ramda-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: python_ramda-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 96.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for python_ramda-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 ba6a058b6e338c13733390b4b36a5b05cc08d0d03a0510febb2890d86dbd8af7
MD5 b6c1c0395a9a22bb23ba2b4e7de3abce
BLAKE2b-256 1f04bee183633cdcf91e9f641ede734cf5e6a4723a6fd25799df8d2ffb1f2b7f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page