I don't have a particular coding problem on hand this is just an excercise to improve my thought process.
Few months back I started learning about functional programming( mostly in R) and I fell in love with it. Once in a while I try to think of problems that might be(correction: that I might find) difficult to solve in FP. Recently I thought of a situation where I might be more interested coding using imperative programming.
It seems to me that all higher order functions like map or reduce will iterate over whole list provided to them which makes sense. How would you avoid iterating over whole list in functional programming for whatever reason - ex. list is too long, list is actually an infinite series, evaluating each item is very expensive etc.
So to make this problem more specific lets say I have an array and I want to return every member from zeroth member to first element whose value is greater than 10, but I want to stop searching through the list once I find that element greater than 10.
How would you solve this?
Example array: 1 2 3 1 2 3 11 1 2 3 1 2 3
I'm not looking for answer specifically in R I've seen enough Haskell & Scala to usually make sense of the code.
EDIT:
I forgot to mention why I would rather use imperative programing here.
I find that it is easier to stop iterating through array above with while
/until
loops because once I reach my terminating condition interpreter exits the loop, but map does not retain information about previous elements, and reduce solves that but I continues to iterate to the end of my list.
takeWhile (<= 10)
in Haskell? I'm not sure what's your question. What does it have to do with "best practices", and why would that be hard in a functional programming language?takeWhile
untill now but after quick google search I can see what you're getting at. Thanks