One of the big changes from Objective-C to Swift is that you can define methods in enums and structs. How can I effectively use that to my advantage. I want to know when can I use this to my advantage with respects to creating an efficient data structure and writing cleaner code.
Structs in swift are quite similar to classes, the only difference really is that when structs are passed as parameters or assigned to variables, they are copied instead of referenced. see this answer and this answer for more detail
otherwise, you should first check out the documentation
Related
I am using System.Linq.Dynamic.Core to parse custom statistical templates, and was wondering if it is possible to somehow extend the library's functionality to parse more mathematical functions. Specifically, I needed in this instance to calculate the absolute value of a variable. I have managed to do this with the already supported "iif" function (i.e. "iif(a>-a, a, -a)"), but I was wondering if there is a way to extend the library to add an "abs()" function, and similarly other functions I may need in the future (such as square root etc).
Any pointers to the right direction?
The System.Linq.Dynamic.Core library is not really designed for this extensibility.
However, you can take a look at the System.Linq.Dynamic.Core.Parser.ExpressionParser.cs for examples, like the IIF you already mention.
Why cannot we change the instance properties from within the instance methods in Swift for the Value Types like struct, enums? Why does the same thing works for reference types like Classes?
Doing mutating func in struct makes it act like a class instance method?
The design of Swift was heavily influenced by functional programming languages such as Haskell, F#, Scala, Erlang, etc. One of the core principles of functional programming is that data is immutable. Functions operate on the data and produce new values as a result rather than modifying the data in place which eliminates side effects. These influences are seen in many places in Swift such as immutable value types, the map function, lambda functions, optional values, and pattern matching (switch in Swift).
new to F#
i need to store a bunch of lists of objects according to a float number where the collection of lists are sorted according to the float number. I know in C# i would use
SortedDictionary<float, List<obj>>
as the implementation is a red black tree, allowing for log(n) insert and search. But whats the best way to attack the situation in F#. I attempted to use SortedDitionary but i can't refer to SortedDictionary[int] to find the value so it renders it as useless essentially (i could be doing it wrong).
thanks for the help
The syntax is
sorteddictionary.[int]
then it works as you would expect
The first thig to do is read Okasaki's book Purely Functional Data Structures
It has ML implementations that may help you
You can use sorteddictionary.[int] as John Palmer already said but it may be worth pointing out that the F# standard library includes a purely functional sorted dictionary collection called Map.
Has anyone implemented a Set class in ActionScript? Specifically, a Set which is similar to Python's set implementation (unordered, unique, O(1) membership, etc).
I'd like to be able to iterate over it using for each, perform operations like union and intersection and get a list of all the contained items… Which would all be possible to implement using Dictionary and Proxy, but I'd rather not reimplement it if someone's already done the heavy lifting.
This looks like a decent enough implementation.
Link to Collection class
Way overkill, but polygonal_ds is a very optimized set of data structures you can use from AS3.
i am kinda confused reading the definition between the two. Can they actually intersect in terms of definition? or am i completely lost? Thanks.
Closures, as the word tends to be used, are just functions (or blocks of code, if you like) that you can treat like a piece of data and pass to other functions, etc. (the "closed" bit is that wherever you eventually call it, it behaves just as it would if you called it where it was originally defined). A monad is (roughly) more like a context in which functions can be chained together sequentially, and controls how data is passed from one function to the next.
They're quite different, although monads will often use closures to capture logic.
Personally I would try to get solid on the definition of closures (essentially a piece of logic which also captures its environment, i.e. local variables etc) before worrying about monads. They can come later :)
There are various questions about closures on Stack Overflow - the best one to help you will depend on what platform you're working on. For instance, there's:
What are closures in .NET?
Function pointers, closures and lambda
Personally I'm only just beginning to "grok" monads (thanks to the book I'm helping out on). One day I'll get round to writing an article about them, when I feel I understand them well enough :)
A "closure" is an object comprising 1) a function, and 2) the values of its free variables where it's constructed.
A "monad" is a class of functions that can be composed in a certain way, i.e. by using associated bind and return higher-order function operators, to produce other functions.
I think monads are a little more complicated than closures because closures are just blocks of code that remember something from the point of their definitions and monads are a construct for "twisting" the usual function composition operation.