Hello people!
Since I'm beginner in J I've decided to solve a simple task using this language, in particular implementing the bubblesort algorithm. I know it's not idiomatically to solve such kind of problem in functional languages, because it's naturally solved using array element transposition in imperative languages like C, rather th...
In one of my first attempts to create functional code, I ran into a performance issue.
I started with a common task - multiply the elements of two arrays and sum up the results:
var first:Array[Float] ...
var second:Array[Float] ...
var sum=0f;
for(ix<-0 until first.length) sum += first(ix) * second(ix);
Here is how I reformed t...
When thinking in a functional mindset, given that functions are supposed to be pure, one can conclude any function with no arguments is basically just a value.
However, reallity gets in the way, and with different inputs, I might not need a certain function, and if that function is computationally expensive, I'd like to not evaluate it i...
is there a function like a F#'s Seq.scan() in python?
i want to do some cumsum() or cumproduct() kind of things without looping.
...
This isn't necessarily a Scala question, it's a design question that has to do with avoiding mutable state, functional thinking and that sort. It just happens that I'm using Scala.
Given this set of requirements:
Input comes from an essentially infinite stream of random numbers between 1 and 10
Final output is either SUCCEED or FAIL
T...
Most of the problems I have to solve in my job as a developer have to do with data modeling.
For example in a OOP Web Application world I often have to change the data properties that are in a object to meet new requirements.
If I'm lucky I don't even need to programmatically add new "behavior" code (functions,methods). Instead I can de...
I have a function that will create a select where clause, but right now everything has to be a string.
I would like to look at the variable passed in and determine what type it is and then treat it properly.
For example, numeric values don't have single quotes around them, option type will either be null or have some value and boolean ...
I've been using Haskell for several months, and I love it—it's gradually become my tool of choice for everything from one-off file renaming scripts to larger XML processing programs. I'm definitely still a beginner, but I'm starting to feel comfortable with the language and the basics of the theory behind it.
I'm a lowly graduate studen...
(callcc (fun k -> k 7)) + 3
(callcc (fun k -> 7)) + 3
What do each of these evaluate to and why?
...
I have some mutable scala code that I am trying to rewrite in a more functional style. It is a fairly intricate piece of code, so I am trying to refactor it in pieces. My first thought was this:
def iterate(count:Int,d:MyComplexType) = {
//Generate next value n
//Process n causing some side effects
return iterate(count - 1, n)
}...
I have a very large List[A] and a function f: List[A] => List[B]. I would like to split my original list into sub-lists with a maximum size, apply the function to each sublist in turn and then unsplit the result into one big List[B]. This pretty easy:
def split[T](l : List[T], max : Int) : List[List[T]] = //TODO
def unsplit[T](l : List...
I've read some texts about declarative/functional programming (languages), tried out Haskell as well as written one myself. From what I've seen, functional programming has several advantages over the classical imperative style:
Stateless programs; No side effects
Concurrency; Plays extremely nice with the rising multi-core technology
P...
I'm trying to define a function, factorize, which uses structural type constraints (requires static members Zero, One, +, and /) similar to Seq.sum so that it can be used with int, long, bigint, etc. I can't seem to get the syntax right, and can't find a lot of resources on the subject. This is what I have, please help.
let inline fac...
There are a lot of impressive Boost libraries such as Boost.Lambda or Boost.Phoenix which go a long way towards making C++ into a truly functional language. But is there a straightforward way to create a composite function from any 2 or more arbitrary functions or functors?
If I have: int f(int x) and int g(int x), I want to do someth...
I have some types that extend a common type, and these are my models.
I then have DAO types for each model type for CRUD operations.
I now have a need for a function that will allow me to find an id given any model type, so I created a new type for some miscellaneous functions.
The problem is that I don't know how to order these types...
In short, this works:
[1, 2, 3].reduce(function (a, b) { return Math.max(a, b); });
=> 3
But this doesn't:
[1, 2, 3].reduce(Math.max);
=> NaN
Pure puzzlement.
This is in Firefox 3.5.9, which I presume is using the mozilla standard implementation of reduce, FWIW.
...
The following simple function applies a given monadic function iteratively until it hits a Nothing, at which point it returns the last non-Nothing value. It does what I need, and I understand how it works.
lastJustM :: (Monad m) => (a -> m (Maybe a)) -> a -> m a
lastJustM g x = g x >>= maybe (return x) (lastJustM g)
As part of my self...
For my Algorithms & Data Structures class, I've been tasked with implementing a splay tree in Haskell. My algorithm for the splay operation is as follows:
If the node to be splayed is the root, the unaltered tree is returned.
If the node to be splayed is one level from the root, a zig operation is performed and the resulting tree is re...
I've coded an experimental function which makes passed objects chainable by using high order functions. It's name is "chain" for now, and here is a usage example;
chain("Hello World")
(print) // evaluates print function by passing "Hello World" object.
(console.log,"Optional","Parameters")
(returnfrom) // returns "Hello World"
I...
While creating a map of String to partial functions I ran into unexpected behavior. When I create a partial function as a map element it works fine. When I allocate to a val it invokes instead. Trying to invoke the check generates an error. Is this expected? Am I doing something dumb? Comment out the check() to see the invocation. ...