kandi background
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Functional Programming open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Functional Programming

ramda

v0.28.0

mostly-adequate-guide

c5844c2

scala

Scala 2.13.8

fantasy-land

Version 5.0.0

fp-ts

2.11.10

ramda

v0.28.0

mostly-adequate-guide

c5844c2

scala

Scala 2.13.8

fantasy-land

Version 5.0.0

fp-ts

2.11.10

Popular Libraries in Functional Programming

ramda

by ramda javascript

star image 21915 MIT

:ram: Practical functional Javascript

mostly-adequate-guide

by MostlyAdequate javascript

star image 20991 NOASSERTION

Mostly adequate guide to FP (in javascript)

scala

by scala scala

star image 13628 Apache-2.0

Scala 2 compiler and standard library. For bugs, see scala/bug

guides

by thoughtbot ruby

star image 8955

A guide for programming in style.

fantasy-land

by fantasyland javascript

star image 8895 MIT

Specification for interoperability of common algebraic structures in JavaScript

Learning-SICP

by DeathKing ruby

star image 8398

MIT视频公开课《计算机程序的构造和解释》中文化项目及课程学习资料搜集。

awesomo

by lk-geimfari python

star image 7943 NOASSERTION

A list of cool open source projects written in C, C++, Clojure, Lisp, Elixir, Erlang, Elm, Golang, Haskell, JavaScript, Lua, OCaml, Python, R, Ruby, Rust, Scala, etc.

fp-ts

by gcanti typescript

star image 7696 MIT

Functional programming in TypeScript

bacon.js

by baconjs typescript

star image 6398 MIT

Functional reactive programming library for TypeScript and JavaScript

ramda

by ramda javascript

star image 21915 MIT

:ram: Practical functional Javascript

mostly-adequate-guide

by MostlyAdequate javascript

star image 20991 NOASSERTION

Mostly adequate guide to FP (in javascript)

scala

by scala scala

star image 13628 Apache-2.0

Scala 2 compiler and standard library. For bugs, see scala/bug

guides

by thoughtbot ruby

star image 8955

A guide for programming in style.

fantasy-land

by fantasyland javascript

star image 8895 MIT

Specification for interoperability of common algebraic structures in JavaScript

Learning-SICP

by DeathKing ruby

star image 8398

MIT视频公开课《计算机程序的构造和解释》中文化项目及课程学习资料搜集。

awesomo

by lk-geimfari python

star image 7943 NOASSERTION

A list of cool open source projects written in C, C++, Clojure, Lisp, Elixir, Erlang, Elm, Golang, Haskell, JavaScript, Lua, OCaml, Python, R, Ruby, Rust, Scala, etc.

fp-ts

by gcanti typescript

star image 7696 MIT

Functional programming in TypeScript

bacon.js

by baconjs typescript

star image 6398 MIT

Functional reactive programming library for TypeScript and JavaScript

Trending New libraries in Functional Programming

ts-pattern

by gvergnaud typescript

star image 2569 MIT

🎨 The exhaustive Pattern Matching library for TypeScript, with smart type inference.

pancake-frontend

by pancakeswap typescript

star image 1679 GPL-3.0

:pancakes: Pancake main features (farms, pools, IFO, lottery, profiles)

monio

by getify javascript

star image 884 MIT

The most powerful IO monad implementation in JS, possibly in any language!

android-startup

by idisfkj kotlin

star image 775 Apache-2.0

🔥The Android Startup library provides a straightforward, performant way to initialize components at the application startup. Both library developers and app developers can use Android Startup to streamline startup sequences and explicitly set the order of initialization.

ultimate_rust_crash_course

by CleanCut rust

star image 554 MIT

Rust Programming Fundamentals - one course to rule them all, one course to find them...

datatype99

by Hirrolot c

star image 518 MIT

Algebraic data types for C99

metalang99

by Hirrolot c

star image 432 MIT

Full-blown preprocessor metaprogramming

ts-belt

by mobily typescript

star image 392 MIT

🔧 Fast, modern, and practical utility library for FP in TypeScript.

magic-in-ten-mins

by niltok html

star image 363 CC-BY-4.0

十分钟魔法练习

ts-pattern

by gvergnaud typescript

star image 2569 MIT

🎨 The exhaustive Pattern Matching library for TypeScript, with smart type inference.

pancake-frontend

by pancakeswap typescript

star image 1679 GPL-3.0

:pancakes: Pancake main features (farms, pools, IFO, lottery, profiles)

monio

by getify javascript

star image 884 MIT

The most powerful IO monad implementation in JS, possibly in any language!

android-startup

by idisfkj kotlin

star image 775 Apache-2.0

🔥The Android Startup library provides a straightforward, performant way to initialize components at the application startup. Both library developers and app developers can use Android Startup to streamline startup sequences and explicitly set the order of initialization.

ultimate_rust_crash_course

by CleanCut rust

star image 554 MIT

Rust Programming Fundamentals - one course to rule them all, one course to find them...

datatype99

by Hirrolot c

star image 518 MIT

Algebraic data types for C99

metalang99

by Hirrolot c

star image 432 MIT

Full-blown preprocessor metaprogramming

ts-belt

by mobily typescript

star image 392 MIT

🔧 Fast, modern, and practical utility library for FP in TypeScript.

magic-in-ten-mins

by niltok html

star image 363 CC-BY-4.0

十分钟魔法练习

Top Authors in Functional Programming

1

PacktPublishing

27 Libraries

554

2

gcanti

18 Libraries

10630

3

typelevel

17 Libraries

12142

4

alvinj

16 Libraries

93

5

zio

16 Libraries

1624

6

47degrees

15 Libraries

919

7

gvolpe

12 Libraries

509

8

scala-exercises

11 Libraries

3002

9

arrow-kt

11 Libraries

5214

10

ThoughtWorksInc

10 Libraries

602

1

27 Libraries

554

2

18 Libraries

10630

3

17 Libraries

12142

4

16 Libraries

93

5

16 Libraries

1624

6

15 Libraries

919

7

12 Libraries

509

8

11 Libraries

3002

9

11 Libraries

5214

10

10 Libraries

602

Trending Kits in Functional Programming

No Trending Kits are available at this moment for Functional Programming

Trending Discussions on Functional Programming

    How do purely functional languages handle index-based algorithms?
    Functional Programming: How does one create a new column to a dataframe that contains a multiindex column?
    Functional Programming in Kotlin: Counting elements in list by using fold
    Are java streams able to lazilly reduce from map/filter conditions?
    Two recursive functions and stackoverflow errors in javascript/nodeJs. Understanding the differences
    Using '|' (pipe) operator with std::views does not compile
    Apply a filter on a multilevel object based on a different object
    What is the relationship between monad functions dist and join in Haskell?
    How to Log in F# in a functional way using NLog
    Idiomatically moving/sorting values from one Vec into another

QUESTION

How do purely functional languages handle index-based algorithms?

Asked 2022-Apr-05 at 12:51

I have been trying to learn about functional programming, but I still struggle with thinking like a functional programmer. One such hangup is how one would implement index-heavy operations which rely strongly on loops/order-of-execution.

For example, consider the following Java code:

1public class Main {
2    public static void main(String[] args) {
3        List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9);
4        System.out.println("Nums:\t"+ nums);
5        System.out.println("Prefix:\t"+prefixList(nums));
6    }
7  
8    private static List<Integer> prefixList(List<Integer> nums){
9      List<Integer> prefix = new ArrayList<>(nums);
10      for(int i = 1; i < prefix.size(); ++i)
11        prefix.set(i, prefix.get(i) + prefix.get(i-1));
12      return prefix;
13    }
14}
15/*
16System.out: 
17Nums:   [1, 2, 3, 4, 5, 6, 7, 8, 9]
18Prefix: [1, 3, 6, 10, 15, 21, 28, 36, 45]
19*/
20

Here, in the prefixList function, the nums list is first cloned, but then there is the iterative operation performed on it, where the value on index i relies on index i-1 (i.e. order of execution is required). Then this value is returned.

What would this look like in a functional language (Haskell, Lisp, etc.)? I have been learning about monads and think they may be relevant here, but my understanding is still not great.

ANSWER

Answered 2022-Mar-07 at 21:17

This is not an index-heavy operation, in fact you can do this with a one-liner with scanl1 :: (a -> a -> a) -> [a] -> [a]:

1public class Main {
2    public static void main(String[] args) {
3        List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9);
4        System.out.println("Nums:\t"+ nums);
5        System.out.println("Prefix:\t"+prefixList(nums));
6    }
7  
8    private static List<Integer> prefixList(List<Integer> nums){
9      List<Integer> prefix = new ArrayList<>(nums);
10      for(int i = 1; i < prefix.size(); ++i)
11        prefix.set(i, prefix.get(i) + prefix.get(i-1));
12      return prefix;
13    }
14}
15/*
16System.out: 
17Nums:   [1, 2, 3, 4, 5, 6, 7, 8, 9]
18Prefix: [1, 3, 6, 10, 15, 21, 28, 36, 45]
19*/
20prefixList = scanl1 (+)
21

indeed, for the list of Nums, we get:

1public class Main {
2    public static void main(String[] args) {
3        List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9);
4        System.out.println("Nums:\t"+ nums);
5        System.out.println("Prefix:\t"+prefixList(nums));
6    }
7  
8    private static List<Integer> prefixList(List<Integer> nums){
9      List<Integer> prefix = new ArrayList<>(nums);
10      for(int i = 1; i < prefix.size(); ++i)
11        prefix.set(i, prefix.get(i) + prefix.get(i-1));
12      return prefix;
13    }
14}
15/*
16System.out: 
17Nums:   [1, 2, 3, 4, 5, 6, 7, 8, 9]
18Prefix: [1, 3, 6, 10, 15, 21, 28, 36, 45]
19*/
20prefixList = scanl1 (+)
21Prelude> prefixList [1 .. 9]
22[1,3,6,10,15,21,28,36,45]
23

scanl1 takes the first item of the original list as initial value for the accumulator, and yields that. Then each time it takes the accumulator and the next item of the given list, and sums these up as new accumulator, and yields the new accumulator value.

Often one does not need indexing, but enumerating over the list is sufficient. Imperative programming languages often work with for loops with indexes, but in many cases these can be replaced by foreach loops that thus do not take the index into account. In Haskell this also often helps to make algorithms more lazy.

If you really need random access lookups, you can work with data structures such as defined in the array and vector packages.

Source https://stackoverflow.com/questions/71387267

Community Discussions contain sources that include Stack Exchange Network

    How do purely functional languages handle index-based algorithms?
    Functional Programming: How does one create a new column to a dataframe that contains a multiindex column?
    Functional Programming in Kotlin: Counting elements in list by using fold
    Are java streams able to lazilly reduce from map/filter conditions?
    Two recursive functions and stackoverflow errors in javascript/nodeJs. Understanding the differences
    Using '|' (pipe) operator with std::views does not compile
    Apply a filter on a multilevel object based on a different object
    What is the relationship between monad functions dist and join in Haskell?
    How to Log in F# in a functional way using NLog
    Idiomatically moving/sorting values from one Vec into another

QUESTION

How do purely functional languages handle index-based algorithms?

Asked 2022-Apr-05 at 12:51

I have been trying to learn about functional programming, but I still struggle with thinking like a functional programmer. One such hangup is how one would implement index-heavy operations which rely strongly on loops/order-of-execution.

For example, consider the following Java code:

1public class Main {
2    public static void main(String[] args) {
3        List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9);
4        System.out.println("Nums:\t"+ nums);
5        System.out.println("Prefix:\t"+prefixList(nums));
6    }
7  
8    private static List<Integer> prefixList(List<Integer> nums){
9      List<Integer> prefix = new ArrayList<>(nums);
10      for(int i = 1; i < prefix.size(); ++i)
11        prefix.set(i, prefix.get(i) + prefix.get(i-1));
12      return prefix;
13    }
14}
15/*
16System.out: 
17Nums:   [1, 2, 3, 4, 5, 6, 7, 8, 9]
18Prefix: [1, 3, 6, 10, 15, 21, 28, 36, 45]
19*/
20

Here, in the prefixList function, the nums list is first cloned, but then there is the iterative operation performed on it, where the value on index i relies on index i-1 (i.e. order of execution is required). Then this value is returned.

What would this look like in a functional language (Haskell, Lisp, etc.)? I have been learning about monads and think they may be relevant here, but my understanding is still not great.

ANSWER

Answered 2022-Mar-07 at 21:17

This is not an index-heavy operation, in fact you can do this with a one-liner with scanl1 :: (a -> a -> a) -> [a] -> [a]:

1public class Main {
2    public static void main(String[] args) {
3        List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9);
4        System.out.println("Nums:\t"+ nums);
5        System.out.println("Prefix:\t"+prefixList(nums));
6    }
7  
8    private static List<Integer> prefixList(List<Integer> nums){
9      List<Integer> prefix = new ArrayList<>(nums);
10      for(int i = 1; i < prefix.size(); ++i)
11        prefix.set(i, prefix.get(i) + prefix.get(i-1));
12      return prefix;
13    }
14}
15/*
16System.out: 
17Nums:   [1, 2, 3, 4, 5, 6, 7, 8, 9]
18Prefix: [1, 3, 6, 10, 15, 21, 28, 36, 45]
19*/
20prefixList = scanl1 (+)
21

indeed, for the list of Nums, we get:

1public class Main {
2    public static void main(String[] args) {
3        List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9);
4        System.out.println("Nums:\t"+ nums);
5        System.out.println("Prefix:\t"+prefixList(nums));
6    }
7  
8    private static List<Integer> prefixList(List<Integer> nums){
9      List<Integer> prefix = new ArrayList<>(nums);
10      for(int i = 1; i < prefix.size(); ++i)
11        prefix.set(i, prefix.get(i) + prefix.get(i-1));
12      return prefix;
13    }
14}
15/*
16System.out: 
17Nums:   [1, 2, 3, 4, 5, 6, 7, 8, 9]
18Prefix: [1, 3, 6, 10, 15, 21, 28, 36, 45]
19*/
20prefixList = scanl1 (+)
21Prelude> prefixList [1 .. 9]
22[1,3,6,10,15,21,28,36,45]
23

scanl1 takes the first item of the original list as initial value for the accumulator, and yields that. Then each time it takes the accumulator and the next item of the given list, and sums these up as new accumulator, and yields the new accumulator value.

Often one does not need indexing, but enumerating over the list is sufficient. Imperative programming languages often work with for loops with indexes, but in many cases these can be replaced by foreach loops that thus do not take the index into account. In Haskell this also often helps to make algorithms more lazy.

If you really need random access lookups, you can work with data structures such as defined in the array and vector packages.

Source https://stackoverflow.com/questions/71387267

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Functional Programming

Tutorials and Learning Resources are not available at this moment for Functional Programming

Share this Page

share link

Get latest updates on Functional Programming