functional-programming

Are there any Clojure Principles ?

Are there any Principles for Clojure ? a. Like the S.O.L.I.D. Object-Oriented Design Principles for OO languages like Java ? b. or others more heuristic, like "Tell don't ask", "Favor Composition vs Inheritance", "Talk to Interfaces" ? Are there any design patterns (for flexible code) ? What is the counter part of the basic of funct...

Am I properly using function composition?

In an effort to understand the capabilities of functional programming I put together a few basic functions that you can compose together to build complex regular expressions. Now after some testing I have found this works but you can write some horrible code in any language that will work. Is this the kind of code you would find a profes...

How can I convert this foldLeft : Double expression to use Option[Double] instead?

Can anyone help this Scala newbie? Previously, we summed a number of quantities in a list of entities with those quantities with: sum = entities.foldLeft(0.0)(_ + _.quantity) Now the quantity is an Option[Double], and so is the sum. How can I convert this using idiomatic Scala? If any entity's quantity is None then the sum should als...

How does one prove the equivalence of two types and that a signature is singly-inhabited?

Anyone who has been following Tony Morris' blog and scala exercises, will know that these two type signatures are equivalent: trait MyOption1[A] { //this is a catamorphism def fold[B](some : A => B, none : => B) : B } And: trait MyOption2[A] { def map[B](f : A => B) : MyOption2[B] def getOrElse[B >: A](none : => B) : B } F...

Is ORM a problem specific to object oriented programming?

Object-Relational Mapping, ORM is a problem that has to be solved in all applications that is implemented in an object oriented programming language and use a relational database. But isn't the problem the same if you are using structs to map relational databases in C? And tuples/records in a functional programming language? Or am I mis...

Path to Become a Better F# Programmer

I would like to hear from you folks that have achieved a high-level of proficiency in F# (and also in functional programming in general too) what should be my steps from now on to become a better/professional F# programmer? I already know much of the F# syntax and have some years of experience with C++. My goal is, as an engineer and ma...

Standard ML: Return different types

I need to return a different value based on the function passed into another function. So, given: fun inc x = x + 1; And: fun double [] = [] | double (h::t) = 2*h::double (t); You should be able to call the function I'm working on with either. Example call (the function I'm making is named test): test (inc, 5); - And it would return...

"filter" higher order function in C++

Does C++ standard library and/or Boost have anything similar to the filter function found in functional languages? The closest function I could find was std::remove_copy_if but it seems to be doing the opposite of what I want. Does boost::lambda have any function to get a negated version of my predicate (similar to not in Haskell)? I co...

Parsing Text Data File With Linq

I have a large text file of records, each delimited by a newline. Each record is prefixed by a two digit number which specifies it's type. Here's an example: .... 30AA ALUMINIUM ALLOY LMELMEUSD2.00 0.35 5101020100818 40AADFALUMINIUM ALLOY USD USD 100 1 0.20000 1.00 0 100 140003 50201008180.999993 0.00 0.0...

Scheme: Using only R6RS, how do I determine a flonum's mantissa and exponent

Is this possible to extract mantissa and exponent from a float in major R6RS Scheme implementations so that: v = f x b^e f - mantissa b - base e - exponent For example: 3.14 = 0.785 x 2^2 If it's not supported, I'd like to have access to flonum's (IEEE 754) bits directly to approach the problem of extracting the above values, but I'v...

Model inheritance using functional programming style data types

I've been using F# recently and tried to code in a functional way rather than doing OOP all over again in a different syntax. I've now run into a problem that I could solve with a mix of inheritance and discriminated unions but for which I'm trying to find a pure functional style representation. What I want to model is something like th...

How to control the space between boxes in rebol draw ?

Question update: I'm almost there, just missing dotted line style for the grid. grid: [1100 600] step-grid: 5 max-n-points: grid/1 / step-grid x-axis-border: 20 Y-margin: 10 max-random: 1000 n-points: 300 get-random-data: func[n p][ block: copy [] repeat i n [ append block RANDOM p ] block ] get-extremes: func[block][ ...

scala return on first Some in list

I have a list l:List[T1] and currently im doing the following: myfun : T1 -> Option[T2] val x: Option[T2] = l.map{ myfun(l) }.flatten.find(_=>true) The myfun function returns None or Some, flatten throws away all the None's and find returns the first element of the list if any. This seems a bit hacky to me. Im thinking that there mig...

Can we define a higher-kinded type-level identity function in Scala?

In Scala we can define the type-level identity function for lower-kinded types like so, type Id[A] = A Can we also define something similar for higher-kinded types? Ie. can we fill in the blanks in, type HKId[A[...]] = ... so that something similar to HKId[List] gets us back to the List type constructor? Binding of free names in t...

What is the most elegant way to deal with an external library with internal state using a function programming language?

I'm currently playing around with Scala development, but I need to integrate with libraries such as box2d to handle physics. The problem is that this requires to depend on an external library which manages its own state. You keep track of the bodies which you pass into the box2d world. To sum up the aspects in play: Box2d manages the s...

Why Rebol engage doesn't work ?

I have added keyboard event but none is detected why ? plot: [ pen green line 5x404 5x440 pen gold fill-pen 0.255.0 box 3x424 7x418 line 10x396 10x422 pen gold fill-pen 0.255.0 box 8x418 12x402 line 15x397 15x436 pen gold fill-pen 255.0.0 box 13x401 17x435 line 20x429 20x447 pen gold fill-pen 255.0.0 box 18x434 22x446 line 25x441 25...

Graduate oppurtunities in functional programming/math area?

Hello, I'm currently applying to graduate schools, with the intention of getting a PhD in a field between math and CS. I am currently majoring in math, but two years ago I took a class in Haskell and got really interested in functional programming in general. I was interested in both the elegance of the ideas, both the abstract ones and...

Find elements in a list that's not in the second list (in scala)

Suppose I have two lists: val a = List('a', 'b', 'c') val b = List('a', 'b', 'c', 'd') I want to get the element which is not in the first list (in this case it's 'd'). I know I can do this with a loop, but is there any fancy functional way to do this quickly in one line? I've been looking at the Scala List API, but could only found ...

Monads vs. Arrows

I'm broadly familiar with the concepts of monads and arrows as used in functional programming. I also understand that they can be used to solve similar kinds of problems. However - I'm still a bit confused about how to select which one to use in any given situation. When should I use monads and when should I use arrows? ...

How to create a panel on the right side of the layout for drawing a chart y-axis ?

I can't see how to position a new panel4 across panel1, panel2, panel3 on the right side for drawing my y-axis: plot: [ pen green line 5x404 5x440 pen gold fill-pen 0.255.0 box 3x424 7x418 line 10x396 10x422 pen gold fill-pen 0.255.0 box 8x418 12x402 line 15x397 15x436 pen gold fill-pen 255.0.0 box 13x401 17x435 line 20x429 20x447...