Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Immutable Data Structures for python, for better OOP Programming and Safer Multi-threading.

License

NotificationsYou must be signed in to change notification settings

rinslow/candyland

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Candyland Sativa

candyland

Immutable Data Structures for python.

Why Should Objects Be Immutable?

This is an incomplete list of arguments in favor of immutability:

  • immutable objects are simpler to construct, test, and use
  • truly immutable objects are always thread-safe
  • they help to avoid temporal coupling
  • their usage is side-effect free (no defensive copies)
  • identity mutability problem is avoided
  • they always have failure atomicity
  • they are much easier to cache
  • they prevent NULL references, which are bad

(Taken fromYegor Bugayenko's blog.)

Currently Supported Data Structures

Upcoming:

  1. Queue
  2. Stack
  3. List
  4. KeyValuePair (Who needs a key-value pair in python?)
  5. Set
  6. PriorityQueue
  7. LinkedList
  8. SortedList
  9. AVLTree
  10. BinarySearchTree
  11. RedBlackTree
  12. Heap
  13. HashTable
  14. Dictionary
  15. BinaryTree

Usage

Stack

fromimmutable.abstractimportStacks=Stack(1,2,3)# Can also: s = Stack() or s = Stack([1, 2, 3])prints.push(7).head()# Prints 7prints.pop().head()# Prints 2prints.count()# Prints 3

Queue

fromimmutable.abstractimportQueueq=Queue(1,2,3)printq.enqueue(7).head()# Prints 1printq.pop().head()# Prints 1printq.count()# Prints 3

List

fromimmutable.abstactimportListl=List(1,2,3)print2inlprintl.reverse().append(3).remove(2).extend([1,2]).pop(1).sort().insert(1,3).count(3)

Dictionary

fromimmutable.abstractimportDictionaryd=Dictionary({1:2,4:5})d+Dictionary([(1,2), (3,4)]).append(4,5).pop(1)+Dictionary.from_keys([1,2],0)

Set

fromimmutable.abstractimportSetprint1in (Set(1,2,3)&Set(3,4,5)| [1,2,0]- [3,4]).add(9).update(List(3,3)).pop().remove(2)

BinaryTree

fromimmutable.treesimportBinaryTreetree=BinaryTree(BinaryTree(BinaryTree(None,None,2),BinaryTree(BinaryTree(None,None,5),None,6),4),BinaryTree(BinaryTree(None,None,10),BinaryTree(BinaryTree(None,None,15),BinaryTree(None,None,18),16),13),8)printtree,repr(tree),13intree,3.14intree,list(tree),iter(tree),tree.add(1).add(2).remove(3).remove(4)forxinBinaryTree.make([1,2,3,-1,4,7,9,14,8,-23]):printx

Heap

fromimmutable.treesimportHeaph1=Heap.make([1,2,3,4,5,6,10,14,11,0,-2],min)h2=Heap([1,2,3],max)h3=Heap([1,2,3])# second parameter is an optional comparator, default is max.printh1.add(1).pop(3).pop(4).add(20).head()

AVLTree

fromimmutable.treesimportAVLTreetree=AVLTree(1,None,None)print2intree.add(1).add(2).pop(4)forxinAVLTree.make([1,2,3,-1,4,7,9,14,8,-23]):printx

     

candyland's True Immutability stems from it's ability to equate based on an object's state and not by it's reference.

About

Immutable Data Structures for python, for better OOP Programming and Safer Multi-threading.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp