How are you trying to make your code functional?

so that you can easily make your program in the future.

+1


source to share


5 answers


I focus on making things immutable. Immutable objects make thinking about multithreaded code much easier than thread-protected objects. An object has one visible state that can be passed between threads without any synchronization. This requires multi-threaded programming.

If you're interested, I've posted most of my work with immutable objects, in particular immutable collections, in the code gallery. Project name RantPack . In the collection area I

  • ImmutableCollection <T>
  • ImmutableMap <TKey, TValue>
  • ImmutableAvlTree <T>
  • ImmutableLinkedList <T>
  • ImmutableArray <T>
  • ImmutableStack <T>
  • ImmutableQueue <T>


There is an additional shim layer, which is (CollectionUtility), which will create wrapper objects that implement BCL interfaces such as IList <T> and ICollection <T>. They cannot fully implement interfaces as they are immutable, but all possible methods are implemented.

Source code (C #) including unit testing is also available on the site.

+3


source


I am programming mostly in Java. I am patiently waiting for the day when closures are added to the language. But since I am still sticking with Java 1.4.2, even if they are added it won't be for me for a long time!



However, my main "functional" way of programming heavily uses the "final" keyword. I try to keep as many classes as possible completely immutable, and for the rest there is a clear distinction between what is temporary and what is immutable.

+1


source


Don't use member variables or global variables. Use a local stack of functions / methods. When a method only uses scoped variables and call parameters and returns all information using out / inout / reference parameters or return values, it is functional.

0


source


Make everything asynchronous. Use immutable objects, messages, etc. Communicate through queues.

0


source


It talks about rubyconf 2008 on the subject, mostly in ruby, but a few concepts remain valid.

http://rubyconf2008.confreaks.com/better-ruby-through-functional-programming-2.html

0


source







All Articles