functional-programming

Control.Parallel compile issue in Haskell

若如初见. 提交于 2020-01-13 07:55:09
问题 The compiler is complaining each time on different example applications of parallel Haskell; with this message: Could not find module `Control.Parallel.Strategies' The ghc compiler command: ghc -threaded -i/sudo/dir/par-modules/3 -cpp -DEVAL_STRATEGIES -eventlog --make parFib.hs Same with simpler ghc -O2 --make -threaded parFib.hs What detail am I overlooking? Am I missing some PATH variable. Imports can look like this: module Main where import System # if defined(EVAL_STRATEGIES) import

Apply multiple functions to the same argument in functional Python

旧时模样 提交于 2020-01-13 07:01:12
问题 I am trying to "invert" map (same function, multiple arguments) in a case where I have multiple function that I want to apply to the same argument. I am trying to find a more function approach to replace the classical arg = "My fixed argument" list_of_functions = [f, g, h] #note that they all have the same signature [fun(arg) for fun in list_of_functions] The only thing I could come up with is map(lambda x: x(arg), list_of_functions) which is not really great. 回答1: You can try: from operator

What are the possibilities to design an API that needs overloads depending on a generic type?

…衆ロ難τιáo~ 提交于 2020-01-13 06:05:53
问题 In a given class Example<A> I need the following two functions to be available: void doSomething(Supplier<A>) <B> void doSomething(Supplier<B>) What are the possibilities to achieve this? I know the following approaches: give the functions different names use a wrapper-type for the second-definition WrapperType.of(Supplier<B>) use a function that converts Example<A> to Example<B> Are there any other approaches? I dislike 1) as it clutters my API. 2) is really verbose as I have to call said

Simple XNA 2D demo: why is my F# version slower than C# version?

血红的双手。 提交于 2020-01-13 04:48:11
问题 When running this XNA application it should display a rotated rectangle that moves from top-left corner to bottom-right corner. It looks like my F# version is noticeably much slower. It seems that the Draw method skips a lot of frames. I am using VS 2012 RC, XNA 4.0, .NET 4.5, F# 3.0. I am trying to make it as functional as possible. What could be the reason for poor performance? C#: class Program { static void Main(string[] args) { using (var game = new FlockGame()) { game.Run(); } } }

Use of the identity function in JavaScript

谁都会走 提交于 2020-01-13 03:56:25
问题 I use the identity function in all my JavaScript programs: function identity(value) { return value; } The reason is that I often need differentiate between primitives types ( undefined , null , boolean , number and string ) and object types ( object and function ) as returned by the typeof operator. I feel using the indentity function for this use case very succuint: if (new identity(value) == value); // value is of an object type if (new identity(value) != value); // value is of a primitive

Use of the identity function in JavaScript

瘦欲@ 提交于 2020-01-13 03:56:23
问题 I use the identity function in all my JavaScript programs: function identity(value) { return value; } The reason is that I often need differentiate between primitives types ( undefined , null , boolean , number and string ) and object types ( object and function ) as returned by the typeof operator. I feel using the indentity function for this use case very succuint: if (new identity(value) == value); // value is of an object type if (new identity(value) != value); // value is of a primitive

Difference between OOP and Functional Programming (scheme) [closed]

泪湿孤枕 提交于 2020-01-13 03:23:32
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 8 years ago . I'm watching a video course/lectures from Stanford. The course is "The Structure and Interpretation of Computer Programs" In the first OOP lecture, the instructor (Brian Harvey) describes an OOP method as one

How Type inference work in presence of Functional Dependencies

早过忘川 提交于 2020-01-12 18:46:53
问题 Consider the code below : {-# LANGUAGE MultiParamTypeClasses,FlexibleInstances,FunctionalDependencies,UndecidableInstances,FlexibleContexts #-} class Foo a c | a -> c instance Foo Int Float f :: (Foo Int a) => Int -> a f = undefined Now when I see the inferred type of f in ghci > :t f > f :: Int -> Float Now If I add the following code g :: Int -> Float g = undefined h :: (Foo Int a) => Int -> a h = g I get the error Could not deduce (a ~ Float) I am not able to understand what has happened

How Type inference work in presence of Functional Dependencies

自古美人都是妖i 提交于 2020-01-12 18:46:17
问题 Consider the code below : {-# LANGUAGE MultiParamTypeClasses,FlexibleInstances,FunctionalDependencies,UndecidableInstances,FlexibleContexts #-} class Foo a c | a -> c instance Foo Int Float f :: (Foo Int a) => Int -> a f = undefined Now when I see the inferred type of f in ghci > :t f > f :: Int -> Float Now If I add the following code g :: Int -> Float g = undefined h :: (Foo Int a) => Int -> a h = g I get the error Could not deduce (a ~ Float) I am not able to understand what has happened

What is an Algebraic Data Type (ADT)?

风格不统一 提交于 2020-01-12 15:16:52
问题 I have heard people talking a lot about Algebraic Data Types (not to be confused with "Abstract Data Types") in functional programming. All I know is that ADT refers to some kind of composite (often recursive) data types like trees or math expressions. In Wikipedia, it's only said that: an algebraic data type is a kind of composite type, i.e., a type formed by combining other types. Two common classes of algebraic types are product types (i.e., tuples and records) and sum types (i.e. tagged