Tag Info

New answers tagged

2

Your friends comment First of all, it is definitely not correct what your friend is stating. Okasaki's book has had a major influence on data structures in several languages. Influence First of all, I think that we can categorize "influence" as one of either: Direct influence: Some programming language is implementing some of the data structures that ...


0

From my experience with boost.bind (which std bind is based on), there’s not much point in capturing the results ahead of time. As noted above the type is rather odd and only exists during compile time. And that’s part of the point. Most of the work behind bind is done at compile time. The initialization of the class is trivial as is the function calling ...


3

I guess you are using c++11 because std::bind is not available in previous editions. From http://en.cppreference.com/w/cpp/utility/functional/bind: template< class F, class... Args > /*unspecified*/ bind( F&& f, Args&&... args ); Obviously the standard committee does not want to hard code the implementation details into the ...


-2

Did you ever hear this aphorism? No man can step in the same river twice. For the second time, it is not the same river, and he is not the same man. Imagine one of your functions writes a 1 to /dev/ttyusb0. In UNIX, this is "just a file", but in real life, this is actually the name of a microcontroller, and by writing a 1 it activates a bread-slicing ...


1

It sounds like you don't know the CS definition of a side effect. A function has a side effect if it has any observable interaction with the system outside of that function. Therefore output is always a side effect, you can observe a change in the hard drive, network, console where ever your output went. Input is similarly a side effect, because it has an ...


0

Side effects mean that, after every IO operation, the state of the system may be changed. Haskell provides a clear divide pure Haskell code and IO code. See : http://learnyouahaskell.com/input-and-output#hello-world ; from the book: "In Haskell, a function can't change some state, like changing the contents of a variable (when a function changes state, ...


9

The Haskell optimizer is allowed to freely manipulate calls to pure functions as long as the result remains the same. For example, if it can see that you are calling sqrt on the same number 100 times, it can cache the returned value and only call it once. If it can see that you never actually use the result of that function, it can choose to not call it at ...


3

I think the problem is that you are a bit unclear on what is meant by a side effect. In a pure function with no side effects, the function gives a result that is a direct result of the input and nothing else. In a function with side effects, the function depends on or changes the state of the outside world. So if you have a function like square(x), then that ...


10

Simplest possible example: printing "Hello, world!" changes the state of the system, because the console now displays "Hello, world!", and earlier it didn't. Not only have you changed the state, it's actually impossible to change it back, since you can't un-get characters from a terminal! That's about the most serious side effect possible.


8

In computer science, a function or expression is said to have a side effect if, in addition to returning a value, it also modifies some state or has an observable interaction with calling functions or the outside world. Reading from a file is an observable interaction with the outside world. It meets the definition of side effect. Reading the 42nd element ...


0

Reading from a stream is a side effect already because the result of functions such as isEOF may return different result after the read than before the read.


1

If you have a shared file handle then reading a file will move that file handle to the position where you have read, and will leave it at that position. If you have two threads with separate file handles to the same file, reading from one will have no noticeable side effect on the other. However in both these cases, memory reading and file reading, there ...


0

Reading from memory does not influence other functions and is therefore side-effect-free. Reading from a file will typically move the file's position pointer, so that when you read again you read the data after what you have already read, so one read function changes the result of other read functions, which is a side effect. If you instead open, read and ...


25

If the memory you access can change, then it is indeed a side effect. For example, in Haskell, the function to access a mutable array (IOArray) has type Ix i => IOArray i e -> i -> IO e (slightly simplified for our purposes). While accessing an immutable array has type Ix i => Array i e -> i -> e The first version returns something ...


2

another version of this code where checked exception is just delayed : public class Cocoon { static <T extends Throwable> T forgetThrowsClause(Throwable t) throws T{ throw (T) t; } public static <X, T extends Throwable> Consumer<X> consumer(PeskyConsumer<X,T> touchyConsumer) throws T { ...


2

Functional programming is not about not having state. It about keeping the functions referential transparency and without side effects. Usually this mean having no state, but that not has to be the case. Referential transparency mean that for a given input parameters the function will always return the same output. No having side effect mean that the ...


0

Functional programming is not about having no state. Instead, all that immutability stuff is about making state explicit. A simple example is adding a list of numbers. In Python, we would do something like this: xs = [1, 2, 3, 4, 5] sum = 0 for x in xs: sum += x (Actually, we'd use the sum builtin, but this is just an example). This loop is stateful, ...


0

Consider the sentence I understand that my methods should not have side affects, and that I should favour immutability. Favour immutability means you should not use mutability when you don't need it, and that you should deal with it in a special way when you do need it. There are many cases in which you can use mutation to compute something, whereas ...


8

You have described an effect system. It’s true that there are other effect systems than monads, but in practice monads give you a lot of expressive power that you would need to reinvent in any practical effect system you might devise. For example: I start by tagging my I/O procedures with an io effect. My pure functions can still throw exceptions, but ...


9

I think you've reinvented monads! Let's look at what we have here, we can "dirty" a pure computation implicitly and use it in an impure one, and we can call impure function from another impure one. That sounds a lot like monads, we can dirty a pure value with return, to call another function from an impure function, we can just use >>= apply :: (a ...


0

I think your statements new AccountPasswordSetter(new AccountAccessor(username)).SetPassword(password); and new AccountAccessor(username).PasswordSetter.Set(password); cannot benefit from FP. It is good that you know Scheme, so I can use FP terminologies to explain: clearly the sole purpose of your above statements is to cause side effect. The ...


0

One stepping stone to fully functional programming is learning a language that has functional concepts built in. You mentioned knowing python, which does have some functional concepts (higher order functions, lambdas, etc.) so it should give you a bit of a jumpstart. For me C# (and LINQ in particular) helped me open my mind to a lot of functional concepts ...


2

Functional programming is a style, which you can use in many languages, although it's more natural in some than others. Out of those you've listed, Python is the "most functional" (it has first-class functions, built-in support for list/dictionary comprehensions, built-in map/reduce/filter functions, etc.). I recommend that, as well as learning Haskell, you ...


4

Just write code. Don't worry about performance unless performance is shown to be a problem. And to the two you have, let me add a third. Create a base object with all of the desired functions as methods, expecting this to have your state. Then have your stateful objects set the base object as their prototype. And now your methods simply exist, and get ...



Top 50 recent answers are included