<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Namc</title>
    <description>The latest articles on DEV Community by Namc (@__namc).</description>
    <link>https://dev.to/__namc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F8299%2F1039eb85-5191-4b64-a75e-153278c8c434.jpg</url>
      <title>DEV Community: Namc</title>
      <link>https://dev.to/__namc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/__namc"/>
    <language>en</language>
    <item>
      <title>Building resilient services</title>
      <dc:creator>Namc</dc:creator>
      <pubDate>Wed, 17 Apr 2019 06:32:57 +0000</pubDate>
      <link>https://dev.to/__namc/building-resilient-services-35j2</link>
      <guid>https://dev.to/__namc/building-resilient-services-35j2</guid>
      <description>&lt;p&gt;Original article published at : &lt;a href="https://namc.in/2019-04-15-retries-timeouts-backoff"&gt;namc.in - Retries, Timeouts and Backoff&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Distributed systems are hard. While we learn a lot about making highly available systems, we often overlook resiliency in system design. &lt;/p&gt;

&lt;p&gt;Sure we have heard about fault-tolerant, but what is "resilience" now? Personally, I like to define it a system's ability to handle and eventually recover from unexpected conditions. There are several ways to go about making your systems resilient to failure, but in this post, we will focus on following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Timeouts &lt;/li&gt;
&lt;li&gt;Retries&lt;/li&gt;
&lt;li&gt;Backoff&lt;/li&gt;
&lt;li&gt;Idempotency in distributed system&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Timeouts
&lt;/h2&gt;

&lt;p&gt;Timeout, simply put, is a maximum period of inactivity between two consecutive data packets. &lt;/p&gt;

&lt;p&gt;I suppose we have worked with database drivers and http-clients at some point of time. All clients/drivers that help connect your service to an external server has a Timeout parameter, which often defaults to zero, or -1. This means that the timeout is undefined, or infinite. &lt;/p&gt;

&lt;p&gt;eg - See the &lt;code&gt;connectTimeout&lt;/code&gt; and &lt;code&gt;socketTimeout&lt;/code&gt; definition &lt;a href="https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html"&gt;Mysql Connector Configuration&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most requests to external servers should have a timeout attached. This is essential when the server is not responding in a timely manner. If this value is not set, and defaults to 0/-1 , your code may hang for minutes or even more. This happens because when you do not receive a response from application server and since your timeout is indefinite, or infintely large, the connection pool remains open. As more requests come in, more connections open up which consequently never close, thereby causing your connection pool to run out. This causes a cascading failure in your application.&lt;/p&gt;

&lt;p&gt;So whenever you are configuring your application to use such connectors, please set an explicit timeout value in their configurations.&lt;/p&gt;

&lt;p&gt;One must also implement timeouts on both, frontend and backend. That is, if a read/write operation is stuck on a rest api or socket for too long, an exception should be raised and connection should be broken. This should signal the backend to cancel the operation and close the connection, thereby preventing indefinitely open connections. &lt;/p&gt;

&lt;h2&gt;
  
  
  Retries
&lt;/h2&gt;

&lt;p&gt;We might need to understand a little about &lt;strong&gt;transient failures&lt;/strong&gt; as we would be using that term frequently. &lt;br&gt;
Simply put, transient failures in your service are temporary glitches, e.g. a network congestion, database overload. Something which is perhaps correct itself given enough cool-off period. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to determine if a failure is transient?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The answer lies in implementation detail of your API/Server responses. If you have a rest API, return a &lt;a href="https://tools.ietf.org/html/rfc7231#section-6.6.1"&gt;503 Service Unavailable&lt;/a&gt; instead of other 5xx/4xx error messages. This will let the client know that the timeout is being caused by "a temporary overload" - not because of a code-level error. &lt;/p&gt;

&lt;p&gt;Retries, although helpful, are a bit notorious if not configured properly. Here's how you can figure out the correct way to use retries. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retry&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;If the error received from the server is transient, e.g. a network packet got currupted while being transmitted, the application could retry the request immediately because the failure is unlike to happen again.&lt;/p&gt;

&lt;p&gt;This is however very agressive, and could prove to be detrimental to your service, which might already be running at capacity, or unavailable completely. It also degrades the application response time as your service would be trying to perform a failing operation continuously. &lt;/p&gt;

&lt;p&gt;If your business logic requires this retry policy, it is best to limit the number of retries to prevent further requests going to the same source. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retry after delay&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the fault is caused due to connectivity failures or due to excess traffic on the network, the application should add a delay period as per business logic before retrying the requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(int attempts = 0; attempts &amp;lt; 5; attempts++)
{
    try
    {
        DoWork();
        break;
    }
    catch { }
    Thread.Sleep(50); // Delay
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When using a library that connects to external services, please check if it implements retry policies, allowing you to configure maximum number of retries, delay between retries etc. &lt;/p&gt;

&lt;p&gt;You can also implement retry policy on server-side, by setting a &lt;a href="https://tools.ietf.org/html/rfc7231#section-7.1.3"&gt;Retry-After&lt;/a&gt; in response header. &lt;/p&gt;

&lt;p&gt;It is also important to log why the operation might be failing. Some times it is due to lack of resources which can handled by adding more instances of that service. Other times it could be due to a memory leak, or a null-pointer exceptions. Hence, it is imperative to add logs and track the performance of your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Backoffs
&lt;/h2&gt;

&lt;p&gt;As we saw above, we can add delay to the retry policy. This delay is often referred to as &lt;em&gt;linear backoff&lt;/em&gt;. This approach may not be the best way to implement a retry policy. &lt;/p&gt;

&lt;p&gt;Consider the case where the fault in your service could be happening because the database is overloaded. It is quite possible that after some retries our request might succeed. It is also possible that consequent requests might be &lt;strong&gt;adding to the overload&lt;/strong&gt; of your database server.Thus, the service would be in overloaded state far longer and will take more time to recover from this state. &lt;/p&gt;

&lt;p&gt;There are several strategies that can be used to solve this problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. &lt;a href="https://en.wikipedia.org/wiki/Exponential_backoff"&gt;Exponential Backoff&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As the name suggests. instead of period delay of, say 5 seconds, between retries, increase the delay between requests exponentially. We do this till we reach the maximum retry limit. If the request continues to fail, let the client know that the request has failed. &lt;/p&gt;

&lt;p&gt;You must also set a limit on how large the delay can be. Exponential backoff might result in setting a delay which is very large, thereby keeping the request socket open indefinitely, and making the thread sleep for "eternity". This will drain the system resources, thereby causing more problems with connection pools.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int delay = 50
for(int attempts = 0; attempts &amp;lt; 5; attempts++)
{
    try
    {
        DoWork();
        break;
    }
    catch { }

    Thread.sleep(delay);
    if (delay &amp;lt; MAX_DELAY)      // MAX_DELAY could depend upon application and business logic 
    {
        delay *= 2;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;One major drawback of exponential backoff is seen in distributed systems, where &lt;strong&gt;requests that backoff at the same time, also retry at the same time&lt;/strong&gt;. This causes clusters of calls. Therefore, instead of reducing number of clients competing in every round, we have now introduced periods when no client is competing. A fixed progression exponential backoff does not reduce the contention much, and generates &lt;strong&gt;peaks of loads&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. &lt;a href="https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/"&gt;Backoff with jitter&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In order to deal with load of spikes as mentioned in exponential backoff, we add &lt;em&gt;jitter&lt;/em&gt; to our backoff strategy. Jitter is a decorrelation strategy, it adds randomness to retry intervals and spreads out the load. This avoids cluster of network calls. &lt;/p&gt;

&lt;p&gt;Jitter is usually not part of any configuration property and needs to implemented by the client. All it only requires is a function which can add randomness, and dynamically calculate the duration to wait before retrying. &lt;/p&gt;

&lt;p&gt;By introducing jitter, the initial group of failing requests may be clustered in a very small window, say 100ms, but with each retry cycle, the cluster of requests spreads into a larger and larger time window, thereby reducing the size of the spike at a given time. The service is likely to be able to handle the requests when spread over a sufficiently large window.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int delay = 50
for(int attempts = 0; attempts &amp;lt; 5; attempts++)
{
    try
    {
        DoWork();
        break;
    }
    catch { }

    Thread.sleep(delay);
    delay *= random.randrange(0, min(MAX_DELAY, delay * 2 ** i)) // just a simple random number generation
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In case of &lt;strong&gt;long-lasting transient failures&lt;/strong&gt;, retries of any kind might not be the best approach. This could be due to a connectivity failure, power outtage (yes, they are very real) etc. The client would end up retrying several times, wasting system resources, further leading to cascading failures across multiple systems. &lt;/p&gt;

&lt;p&gt;So we need a mechanism to determine if the failure is long-lasting, and implement a solution to handle it. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. &lt;a href="https://en.wikipedia.org/wiki/Circuit_breaker_design_pattern"&gt;Circuit Breakers&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Circuit Breaker pattern is useful for handling long-lasting transient failures of a service by determining its availability, preventing the client from retrying requests that are bound to fail.&lt;/p&gt;

&lt;p&gt;Circuit breaker design pattern requires that the state of the connection be retained over a series of requests. Lets look at this &lt;a href="https://github.com/jhalterman/failsafe#circuit-breakers"&gt;Circuit breaker implementation by failsafe&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;CircuitBreaker&lt;/span&gt; &lt;span class="n"&gt;breaker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CircuitBreaker&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withFailureThreshold&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withSuccessThreshold&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;withDelay&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TimeUnit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MINUTES&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;Failsafe&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;breaker&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When everything runs as expected, there are no outtages, the circuit breaker remains in a closed state. &lt;/p&gt;

&lt;p&gt;When a threshold of executation failures occur, the circuit breaker trips and goes into &lt;strong&gt;open&lt;/strong&gt; state, which means, all consequent requests will continue to fail without going through retry logic. &lt;/p&gt;

&lt;p&gt;After a delay (1 minute as mentioned above), the circuit will go into &lt;strong&gt;half-open&lt;/strong&gt; state, just to test if the problem with network call still exists, thereby deciding if the circuit should be closed or opened. If it succeeds, the circuit resets to &lt;strong&gt;closed&lt;/strong&gt; state, else it is set as &lt;strong&gt;open&lt;/strong&gt; again.&lt;/p&gt;

&lt;p&gt;This helps in avoiding cluster of retry executions during long lasting faults, saving system resources. &lt;/p&gt;

&lt;p&gt;While this can be maintained locally in a state variable, you might need an external storage layer if you have a &lt;strong&gt;distributed system&lt;/strong&gt;. In a multi-node setup, the state of application server will need to shared across all instances. In such a scenario, you can use Redis, memcached to keep a record of the availability of external services. Before making any requests to external service, the state of service is queried from the persistent storage. &lt;/p&gt;

&lt;h2&gt;
  
  
  Idempotency in distributed systems
&lt;/h2&gt;

&lt;p&gt;A service is idempotent when clients can make same requests repeatedly while producing same end-result. While the operation would produce same result on the server, it might not give the same response to client.&lt;/p&gt;

&lt;p&gt;In case of REST APIs, you need to remember - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;POST&lt;/strong&gt; is NOT idempotent - POST causes new resources to be created on server. "n" POST requests result in creating "n" new resources on the server. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GET&lt;/strong&gt;, &lt;strong&gt;HEAD&lt;/strong&gt;, &lt;strong&gt;OPTIONS&lt;/strong&gt; and &lt;strong&gt;TRACE&lt;/strong&gt; methods NEVER change the resource state on server. Hence, they are always idempotent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PUT&lt;/strong&gt; requests are idempotent. "n" put requests will overwrite the same resource "n-1" times.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DELETE&lt;/strong&gt; is idempotent because it would return 200 (OK) initially, and 204 (No Content) or 404 (Not Found) on subsequent calls.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why care about idempotent operations?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a distributed system, there are several server and client nodes. If you make a request from Client to Server A which fail, or times out, then you would like to able to simply make that request again, without worrying if the previous request had any side-effects.  &lt;/p&gt;

&lt;p&gt;This is extremely essential in micro-services where a lot of components operate independently. &lt;/p&gt;

&lt;p&gt;Some key benefits of idempotency are - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Minimal complexity&lt;/strong&gt; - No need to worry about side effects, any request can be simply retried, and same end-result is achieved.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easier to implement&lt;/strong&gt; - You would not need to add logic to handle previous failed requests in your retry mechanism. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easier to test&lt;/strong&gt; - Each action result in same result, no surprises. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final notes
&lt;/h2&gt;

&lt;p&gt;We went through a bunch of ways you can build a more fault-tolerant system. However, that is not all. In closing, I would like to add a few pointers you can look into, which might help make your systems more available and tolerant to failures.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In a multi-node setup, if a client is retrying several times, the requests are likely to hit the same server. When this happens, it is best to give a failure response and making the client try again from scratch.
&lt;/li&gt;
&lt;li&gt;Profile your systems, keep them prepared for the worst. You might want to check out &lt;a href="https://github.com/Netflix/chaosmonkey"&gt;Chaos Monkey by Netlifx&lt;/a&gt; - its a resiliency tool which triggers random failures in your system. This keeps you prepared for faults that might occur, helping you build a resilient system. &lt;/li&gt;
&lt;li&gt;If your systems are under excessive loads for some reason, you can try distributing it by load shedding. Google did a brilliant &lt;a href="https://cloud.google.com/blog/products/gcp/using-load-shedding-to-survive-a-success-disaster-cre-life-lessons"&gt;case study&lt;/a&gt; which can serve as a good starting point. &lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Resources - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.dre.vanderbilt.edu/~schmidt/patterns-ace.html"&gt;Patterns for distributed systems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/azure/architecture/patterns/retry"&gt;Retry Patterns -  Microsoft&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://martinfowler.com/bliki/CircuitBreaker.html"&gt;Martin Fowler Circuit Breaker&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you! ❤&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>SSH Tunneling - Local, Remote &amp; Dynamic</title>
      <dc:creator>Namc</dc:creator>
      <pubDate>Wed, 27 Jun 2018 00:09:30 +0000</pubDate>
      <link>https://dev.to/__namc/ssh-tunneling---local-remote--dynamic-34fa</link>
      <guid>https://dev.to/__namc/ssh-tunneling---local-remote--dynamic-34fa</guid>
      <description>&lt;p&gt;Original article published at : &lt;a href="https://namc.in/2018-06-26-ssh-port-forwarding"&gt;namc.in - SSH Tunneling&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most of us are familiar with SSH (Secure Shell) - a protocol which allows us to securely log onto remote systems. SSH Tunneling (also known as SSH Port Forwarding) is a feature of SSH which forwards encrypted connections between a local and remote system. SSH tunneling works by using the already established SSH connection for sending additional network traffic.&lt;/p&gt;

&lt;p&gt;We're going to look at the three types of port forwarding - local, remote &amp;amp; dynamic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Local port forwarding
&lt;/h2&gt;

&lt;p&gt;Let's see what the man page for SSH tells us about &lt;code&gt;local&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;local&lt;/span&gt;: &lt;span class="nt"&gt;-L&lt;/span&gt; 
Specifies that the given port on the &lt;span class="nb"&gt;local&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;client&lt;span class="o"&gt;)&lt;/span&gt; host is to be forwarded to the given host and port on the remote side.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So generically, the command would look something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-L&lt;/span&gt; sourcePort:forwardToHost:destPort connectToHost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This translates to - connect with ssh to &lt;code&gt;connectToHost&lt;/code&gt;, and forward all connection attempts to the &lt;strong&gt;local&lt;/strong&gt; &lt;code&gt;sourcePort&lt;/code&gt; to port &lt;code&gt;destPort&lt;/code&gt; on the machine called &lt;code&gt;forwardToHost&lt;/code&gt;, which can be reached from the &lt;code&gt;connectToHost&lt;/code&gt; machine. Forwarding can also be done using Unix sockets.&lt;/p&gt;

&lt;p&gt;Say YouTube is blocked on your office network, and you reaaaalllyy wanna watch some kitty videos. So, you can get around it by creating a tunnel through a server which isn't on your network and access YouTube. The above command can be translated to :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-L&lt;/span&gt; 9000:youtube.com:80 user@example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-L&lt;/code&gt; flag here signfies that we're doing local port forwarding. We connect with the &lt;code&gt;user@example.com&lt;/code&gt; machine. We then forward any connection to port 9000 on the local machine to port 80 (which is the default port for HTTP) on &lt;code&gt;youtube.com&lt;/code&gt;. Now, if you open your browser and go to &lt;code&gt;http://localhost:9000&lt;/code&gt; , a request is made to HTTP server listening on &lt;code&gt;youtube.com&lt;/code&gt;. However, you, on your local machine, have no webserver running.&lt;/p&gt;

&lt;p&gt;You still won't be able to the see homepage - so don't worry about that.&lt;/p&gt;

&lt;p&gt;The requests in the browser for &lt;code&gt;localhost:9000&lt;/code&gt; are built with a Host Destination header of localhost value. This request reaches &lt;code&gt;youtube.com&lt;/code&gt; machine. But this request is ignored with an error message - &lt;code&gt;Invalid virtual host ..&lt;/code&gt; because localhost cannot be a domain name on the server which is running &lt;code&gt;youtube&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;To fix this, we change the Host HTTP header enabling remote web server to identify the corresponding destination.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;curl &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Host: youtube.com"&lt;/span&gt; &lt;span class="nt"&gt;-L&lt;/span&gt; localhost:9000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-L&lt;/code&gt; parameter in curl is for following redirects. You should now be able to see the contents of homepage from &lt;code&gt;youtube.com&lt;/code&gt; on &lt;code&gt;localhost:9000&lt;/code&gt; as long as you are connected to &lt;code&gt;user@example.com&lt;/code&gt; machine.&lt;/p&gt;

&lt;p&gt;Also! The good things about SSH tunnels is that they are encrypted, so nobody can see what sites you are visiting - only an SSH connection to your server. (Take that, Office admins!! 😎)&lt;/p&gt;

&lt;p&gt;By default, anyone (even on different machines) can connect to the specified port on the local machine. This can be restricted to programs on the same host by supplying a bind address:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-L&lt;/span&gt; 127.0.0.1:9000:youtube.com:80 user@example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SSH binds to port 9000 on the local machine. Any traffic that comes to this port is sent to the SSH server that listens on &lt;code&gt;user@example.com&lt;/code&gt; - the remote machine. Once received by remote-machine, the traffic is then sent to port 80 of 127.0.0.1, which is &lt;code&gt;user@example.com&lt;/code&gt; itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connecting to a database behind a firewall
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;forwardToHost&lt;/code&gt; host may also refer to the remote machine through which the ssh connection is made (i.e. &lt;code&gt;connectToHost&lt;/code&gt;) - in which case, the value for &lt;code&gt;forwardToHost&lt;/code&gt; becomes 127.0.0.1 or localhost, as its local in the context of already established connection with &lt;code&gt;connectToHost&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-L&lt;/span&gt; 9000:127.0.0.1:80 user@example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An example for this is when you need to connect to a database console, which only allows local connection for security reasons, running PostgreSQL on your server, which by default listens on the port 5432.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-L&lt;/span&gt; 9000:localhost:5432 user@example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command forwards the local port 9000 to the port 5432 on the remote machine. You can connect to that remote PostgreSQL server through the local machine using psql on localhost:9000, simply like :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;psql &lt;span class="nt"&gt;-h&lt;/span&gt; localhost &lt;span class="nt"&gt;-p&lt;/span&gt; 9000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's take a moment here and understand what is actually going on. &lt;/p&gt;

&lt;p&gt;In the YouTube example, &lt;code&gt;9000:youtube.com:80&lt;/code&gt; says - forward my local port 9000 to &lt;code&gt;youtube.com&lt;/code&gt; at port 80. So SSH on your server actually makes a tunnel (connection) between those two ports - one of which lies on your local machine, and another on target machine.&lt;/p&gt;

&lt;p&gt;In the example of database connection, &lt;code&gt;9000:localhost:5432&lt;/code&gt; means localhost from server's perspective, not localhost on your machine. In other words - forward my local port 9000 to port 5432 on the server - because when you're on the server, localhost means server itself. &lt;/p&gt;

&lt;p&gt;Port numbers less than 1024 or greater than 49151 are reserved for the system, and can only be forwarded by root. If you're using port forwarding of any kind, you need to specifiy the destination server, i.e. &lt;code&gt;connectToHost&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Port forwarding is enabled by default. If not, check &lt;code&gt;AllowTcpForwarding&lt;/code&gt; in &lt;code&gt;/etc/ssh/sshd_config&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Remote port forwarding
&lt;/h2&gt;

&lt;p&gt;Going back to the man-page again to see the definition of &lt;code&gt;remote&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;remote: &lt;span class="nt"&gt;-R&lt;/span&gt; 
Specifies that the given port on the remote &lt;span class="o"&gt;(&lt;/span&gt;server&lt;span class="o"&gt;)&lt;/span&gt; host is to be forwarded to the given host and port on the &lt;span class="nb"&gt;local &lt;/span&gt;side.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command would look much like local tunneling's but with an &lt;code&gt;-R&lt;/code&gt; flag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-R&lt;/span&gt; sourcePort:forwardToHost:destPort connectToHost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This translates to - connect with ssh to &lt;code&gt;connectToHost&lt;/code&gt;, and forward all connection attempts to the &lt;strong&gt;remote&lt;/strong&gt; &lt;code&gt;sourcePort&lt;/code&gt; to port &lt;code&gt;destPort&lt;/code&gt; on the machine called &lt;code&gt;forwardToHost&lt;/code&gt;, which can be reached from the &lt;code&gt;connectToHost&lt;/code&gt; machine. Forwarding can also be done using Unix sockets.&lt;/p&gt;

&lt;p&gt;Okay! Let's see an example. &lt;/p&gt;

&lt;p&gt;Say you're developing an application on your local machine and you'd like to show the prototype to your boss. &lt;/p&gt;

&lt;p&gt;In most cases, the ISP doesn't provide you with a public IP address, so you cannot connect your machine directly via the internet. While this problem can be solved by configuring NAT (Network Address Translation) on your router - this might not always work, there's a technical overhead of changing the configuration of your router, and you would need the admin access on your network. &lt;/p&gt;

&lt;p&gt;In such a scenario, you can setup a server on internet which is publicly accessible and has SSH access. Then we tell SSH to make a tunnel that opens up a new port on server, and you connect to it via local port on your machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-R&lt;/span&gt; 9000:localhost:3000 user@example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax here is very similar to local port forwarding, with a single change of &lt;strong&gt;-L&lt;/strong&gt; for &lt;strong&gt;-R&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;SSH will connect to the remote machine - in this case &lt;code&gt;user@example.com&lt;/code&gt;. The flag &lt;strong&gt;-R&lt;/strong&gt; makes ssh listen on the port 9000 of that machine. Once there's a process on the machine connecting to 9000, the ssh server listening on the same machine will transfer that connection to local machine - the machine that initiated the ssh communication - and forward it to localhost on the port 3000. &lt;/p&gt;

&lt;p&gt;Remote port forwarding allows to map a port of the local machine onto the remote server via SSH.&lt;/p&gt;

&lt;p&gt;Another thing which you need to do is to set &lt;code&gt;GatewayPorts&lt;/code&gt;. In your &lt;code&gt;/etc/ssh/sshd_config&lt;/code&gt; , set&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;GatewayPorts &lt;span class="nb"&gt;yes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And restart SSH&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;service ssh restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows the SSH server to bind port 3000 to the wildcard address - such that the port becomes available to the public address of the &lt;code&gt;connectToHost&lt;/code&gt; remote machine. &lt;/p&gt;

&lt;p&gt;You can also set &lt;code&gt;GatewayPorts&lt;/code&gt; to &lt;code&gt;clientspecified&lt;/code&gt; - in which case, the remote port will not bind on the wildcard address. You might need to explicitly specify an empty bind address for binding the wildcard address - which can be done by prefixing remote port with &lt;code&gt;:&lt;/code&gt; sign.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-R&lt;/span&gt; :9000:localhost:3000 user@example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also specify an IP address from which connections to the port are allowed, such that only connections from the given IP address to the given port are allowed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-R&lt;/span&gt; 1.2.3.4:9000:localhost:3000 user@example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your boss will be able to access your application on port 3000 by pointing their browser to &lt;code&gt;connectToHost&lt;/code&gt; IP address on the port 9000.&lt;/p&gt;

&lt;h3&gt;
  
  
  Double forwarding
&lt;/h3&gt;

&lt;p&gt;If the remote server has &lt;code&gt;GatewayPorts&lt;/code&gt; set to &lt;code&gt;no&lt;/code&gt;, with no possibility of changing it - you can execute the remote forwarding above followed by a local forwarding using &lt;code&gt;-g&lt;/code&gt; option, but from the remote server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-g&lt;/span&gt; &lt;span class="nt"&gt;-L&lt;/span&gt; 9000:localhost:3000 user@example.com 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;-g&lt;/strong&gt; allows remote hosts to connect to local forwarded ports and this will make loopback port 3000 on the server accessible on all interfaces on port 9000.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dynamic port forwarding - SOCKS
&lt;/h2&gt;

&lt;p&gt;Dynamic port forwarding allows communication across a range of ports. This port forwarding is created using a &lt;strong&gt;-D&lt;/strong&gt; parameter. This makes SSH acts as a &lt;a href="https://en.wikipedia.org/wiki/SOCKS"&gt;SOCKS proxy server&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There's two kinds of SOCKS protocols - SOCKS4 and SOCKS5. These are basically internet protocols which route packets between a server and a client using proxy server. SOCKS5 uses both TCP and UDP, whereas SOCKS4 uses only TCP. &lt;/p&gt;

&lt;p&gt;A SOCKS proxy is a simple SSH tunnel in which specific applications forward their traffic through the tunnel to the remote server, then the proxy forwards the traffic out to the general internet. Unlike a VPN, SOCKS proxy has to be configured for each application separately on the client machine.&lt;/p&gt;

&lt;p&gt;Dynamic port forwarding can handle connections from multiple ports. It analyses the traffic to determine the destination for given connection. However, you might need to configure programs to use a SOCKS proxy server. &lt;/p&gt;

&lt;p&gt;This can be done by :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-D&lt;/span&gt; 9000 &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="nt"&gt;-C&lt;/span&gt; &lt;span class="nt"&gt;-q&lt;/span&gt; &lt;span class="nt"&gt;-N&lt;/span&gt; connectToHost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;-D&lt;/strong&gt; tells SSH to create a SOCKS tunnel on the the port 9000.&lt;br&gt;
&lt;strong&gt;-f&lt;/strong&gt; forks the process to the background.&lt;br&gt;
&lt;strong&gt;-C&lt;/strong&gt; compresses the data before sending it. &lt;br&gt;
&lt;strong&gt;-q&lt;/strong&gt; enables quiet mode. &lt;br&gt;
&lt;strong&gt;-N&lt;/strong&gt; tells SSH that no command will be sent once the tunnel is up.&lt;/p&gt;

&lt;p&gt;A downside to using proxies is decreased performance and mislabelled errors as they rewrite data packet headers. With SOCKS5 proxy, the server does not rewrite data packet headers - hence being more performant and less prone to data routing errors. Because SOCK5 proxies they are low-level, they can work with any kind of data traffic - program, protocol etc.&lt;/p&gt;
&lt;h2&gt;
  
  
  Closing Tip!
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;-nNT&lt;/code&gt; flags will cause SSH to not allocate a tty and only do the port forwarding. This will prevent the creation of shell everytime you create a tunnel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-nNT&lt;/span&gt; &lt;span class="nt"&gt;-L&lt;/span&gt; 9000:youtube.com:80 user@example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  More reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.trackets.com/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples.html?utm_source=cronweekly.com"&gt;SSH Tunneling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://unix.stackexchange.com/questions/46235/how-does-reverse-ssh-tunneling-work"&gt;UnixSE - how does ssh tunneling work&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://unix.stackexchange.com/a/19624"&gt;UnixSE - ssh port forward to access my home machine from anywhere&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://vimeo.com/54505525"&gt;The Black Magic Of SSH / SSH Can Do That?&lt;/a&gt; - This is a video.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>networking</category>
      <category>programming</category>
      <category>linux</category>
    </item>
    <item>
      <title>What is Currying?</title>
      <dc:creator>Namc</dc:creator>
      <pubDate>Sun, 25 Feb 2018 06:37:10 +0000</pubDate>
      <link>https://dev.to/__namc/what-is-currying--3l2a</link>
      <guid>https://dev.to/__namc/what-is-currying--3l2a</guid>
      <description>&lt;p&gt;Original article published at : &lt;a href="https://namc.in/2018-02-22-currying"&gt;https://namc.in/2018-02-22-currying&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Inspiration
&lt;/h2&gt;

&lt;p&gt;This post is written after I saw a tweet which read &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every functional programming tutorial:&lt;/p&gt;

&lt;p&gt;OK, we're going to talk about currying. Currying is when you break down a function tha-&lt;br&gt;
&lt;em&gt;scrolls down&lt;/em&gt;&lt;br&gt;
const Y = f =&amp;gt; (g =&amp;gt; g(g))(g =&amp;gt; f(x =&amp;gt; g(g)(x)))&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And realized that we need a simpler tutorial to understand currying, the logic behind it, the nuances and the usage. &lt;/p&gt;

&lt;p&gt;We will be using Haskell for this tutorial, but this can be extended to any functional programming language. &lt;/p&gt;

&lt;h2&gt;
  
  
  Higher-order functions!
&lt;/h2&gt;

&lt;p&gt;In mathematics, higher-order functions (HOFs) are called functionals. But to simplify it for general software engineers, HOF is simply a functions that &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;either takes a function as an argument &lt;/li&gt;
&lt;li&gt;or returns a function as a result&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A well-known higher order function is map, which performs an action on each element in a collection, e.g.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Haskell&lt;/span&gt;
&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;-- =&amp;gt; [2, 4, 6, 8, 10]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In usual imperative programming, functions can accept values, like integers and strings (first order functions) and return a value of some other type. &lt;/p&gt;

&lt;p&gt;Consider a function, &lt;code&gt;f&lt;/code&gt; such that -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What if we wanted to generalize the operators in above function? That would make our operators a variable too. Right? Let's see a way to define a more generalized version of &lt;code&gt;f&lt;/code&gt;, where &lt;code&gt;g&lt;/code&gt; and &lt;code&gt;h&lt;/code&gt; represent the operator-functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="c1"&gt;-- returns 1&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="c1"&gt;-- returns 1.5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That was simple! But, we mentioned that HOFs can return a function too. Yes, we can create a function that accepts a function and an argument and returns another function, which accepts an argument and returns a result. For example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;  
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c1"&gt;-- returns 12&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c1"&gt;-- returns 20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, &lt;code&gt;(\m -&amp;gt; m&lt;/code&gt;f&lt;code&gt;n)&lt;/code&gt; construct is an anonymous function of 1 argument &lt;code&gt;m&lt;/code&gt; which applies &lt;code&gt;f&lt;/code&gt; to &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt;. And using this anonymous functions, we can define &lt;code&gt;g h 2&lt;/code&gt; which is a function that accepts one argument &lt;code&gt;g&lt;/code&gt; and operates &lt;code&gt;h&lt;/code&gt; to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Trivia :&lt;/strong&gt; Scheme, according to Wikipedia, was the first language to introduce proper higher-order functions as first-class citizens, however the first mention dates back to Frege in "Funktion und Begriff" (1891).&lt;/p&gt;

&lt;h2&gt;
  
  
  Currying
&lt;/h2&gt;

&lt;p&gt;Currying is a technique that transforms a function of several arguments to a function of a single argument, which returns a function of 1 argument, which returns a functions of 1 argument ... till it returns a value. &lt;/p&gt;

&lt;p&gt;A curried function is one that returns a function as its result. A fully curried function is a one-argument function that either returns an ordinary result or returns a fully curried function. &lt;/p&gt;

&lt;p&gt;Note that a curried function is necessarily a higher-order function, since it returns a function as its result.&lt;/p&gt;

&lt;p&gt;Let's take an example, where we have a function of two args, like (+). But what if you could define the same function, by giving only one argument to it, thereby returning a function, which you could use later to add thid 1st argument, now encased in this new function, to something else.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="c1"&gt;-- would return 18&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="c1"&gt;-- would return 14&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same can be written in javascript like :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How could be make it more abstract? Lets define &lt;code&gt;curry&lt;/code&gt; , which takes a function and an argument, such that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;curry&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;

&lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curry&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c1"&gt;-- returns 15&lt;/span&gt;

&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curry&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c1"&gt;-- returns 50&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we check the type &lt;code&gt;:t&lt;/code&gt; of &lt;code&gt;curry&lt;/code&gt; function defined above, we find,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;curry&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Haskell functions
&lt;/h2&gt;

&lt;p&gt;Haskell curries all functions by default. There are no functions of multiple arguments in Haskell. What you have are only functions of one argument, some of which may return new functions of one arguments. So you can define a multi-argument function as you would in any other language and you automatically get a curried version of it, without having to write lamdas yourself. &lt;/p&gt;

&lt;p&gt;For example, take the function &lt;code&gt;(++)&lt;/code&gt; which concatenates two strings together.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The type definition can be understood as, &lt;code&gt;(++)&lt;/code&gt; accepts one list, and returns a function of type &lt;code&gt;[a] -&amp;gt; [a]&lt;/code&gt;. The resultant function can accept yet another list, and we get a new list of type &lt;code&gt;[a]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you are familiar with other languages, you can correlate &lt;code&gt;f a b c&lt;/code&gt; as Lisp's &lt;code&gt;(((f a) b) c)&lt;/code&gt; or Java's &lt;code&gt;f(a, b, c)&lt;/code&gt; . This makes sense as , in Haskell, the function &lt;code&gt;f&lt;/code&gt; is curried by default. &lt;/p&gt;

&lt;p&gt;However, when you're analyzing types, the association is from right to left, so &lt;code&gt;[a] -&amp;gt; [a] -&amp;gt; [a]&lt;/code&gt; is equivalent to &lt;code&gt;[a] -&amp;gt; ([a] -&amp;gt; [a])&lt;/code&gt;, which means that when you apply an argument to the function, you get back a function of type &lt;code&gt;[a] -&amp;gt; [a]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Similiary, you can analyze &lt;code&gt;map&lt;/code&gt;, which accepts a function &lt;code&gt;(a -&amp;gt; b)&lt;/code&gt; and a list &lt;code&gt;[a]&lt;/code&gt; , and returns a transformed list &lt;code&gt;[b]&lt;/code&gt;, such that function is mapped over all elements of &lt;code&gt;[a]&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt;
&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try the following snippet out in ghci&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;map&lt;/span&gt;
&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"haskell"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"kitty"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"haskell"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"kitty"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Partial application
&lt;/h2&gt;

&lt;p&gt;Partial application is when you call a function with 1 or multiple arguments to get back a function that still accepts arguments. This allows us to write shorter and concise code. &lt;/p&gt;

&lt;p&gt;Partial application can be used as a substitute for anonymous functions. Let's look at some examples.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;take&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;-- can be written as&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;take&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What if you want to use an operator, say &lt;code&gt;(+)&lt;/code&gt; instead of &lt;code&gt;take&lt;/code&gt;? For infix functions, you can partially apply it using &lt;a href="https://wiki.haskell.org/Section_of_an_infix_operator"&gt;sections&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;-- can be written as &lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;-- and &lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;-- can be written as&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In place of &lt;code&gt;(+)&lt;/code&gt; , you can use any binary function to define sections.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;xs&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="n"&gt;elem'&lt;/span&gt; &lt;span class="n"&gt;xs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;-- can be written as&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;elem'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The point to note here is , since every function is curried by default, and accepts one argument only, so sections can be used with any function. For example ,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="kr"&gt;let&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;

&lt;span class="c1"&gt;-- such that f is (+)&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="c1"&gt;-- returns 14&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Currying and Pattern Matching
&lt;/h2&gt;

&lt;p&gt;We take for granted that we can have nested patterns and patterns over more than one term, when in reality for the purposes of a compiler the only thing you can do is branch on the top-level constructor of a single value. So the first stage of the compiler is to turn nested patterns (and patterns over more than one value) into simpler patterns. &lt;/p&gt;

&lt;p&gt;Let's take an example, a naive algorithm might transform your function into something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;myFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
        &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;some_operation&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
        &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;some_operation&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
    &lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;some_operation&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, there are a lot of caveats in the above implementation. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;some_operation&lt;/code&gt; term is repeated a lot&lt;/li&gt;
&lt;li&gt;the function expects both arguments before it will even start to do a case at all&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Refer to &lt;a href="https://www.classes.cs.uchicago.edu/archive/2011/spring/22620-1/papers/pettersson92.pdf"&gt;A Term Pattern-Match Compiler Inspired by Finite Automata Theory&lt;/a&gt; for further discussions on how we can improve it. &lt;/p&gt;

&lt;p&gt;Anyway, in the form below, it should actually be a bit more clear how the currying step happens. We can directly substitute for &lt;code&gt;x&lt;/code&gt; in this expression to look at what &lt;code&gt;myFunc 0&lt;/code&gt; does:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;myFunc&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
        &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;some_operation&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
        &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;some_operation&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
    &lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;some_operation&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this is still a lambda, so no further reduction is done. &lt;/p&gt;

&lt;p&gt;We will need to change the definition of our function if we want GHC to do more computation after supplying only one argument. There's a time/space tradeoff here. So GHC leaves it in the programmer's hands to make this choice. For example, you could explicitly write&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;myFunc&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;some_operation&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="n"&gt;myFunc&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;some_operation&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;

&lt;span class="n"&gt;myFunc&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;`&lt;/span&gt;&lt;span class="n"&gt;some_operation&lt;/span&gt;&lt;span class="p"&gt;`&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then &lt;code&gt;myFunc 0&lt;/code&gt; would reduce to a much smaller expression.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compositions
&lt;/h2&gt;

&lt;p&gt;Compositions and Application operator are very handy for writing concise and flexible code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Composition operator&lt;/strong&gt; &lt;code&gt;(.)&lt;/code&gt; chains functions together. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Application operator&lt;/strong&gt; &lt;code&gt;($)&lt;/code&gt; applies function on the left side to the argument on the right side&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;f $ x&lt;/code&gt; is equivalent to &lt;code&gt;f x&lt;/code&gt;. However &lt;code&gt;($)&lt;/code&gt; has the lowest precedence of all operators, so we can use it to get rid of parentheses: &lt;code&gt;f (g x y)&lt;/code&gt; is equivalent to &lt;code&gt;f $ g x y&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It is also helpful when we need to apply multiple functions to the same argument:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; 

&lt;span class="c1"&gt;-- would yield &lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following expressions are all equivalent&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;   &lt;span class="c1"&gt;-- 1&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;-- 2&lt;/span&gt;

&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;   &lt;span class="c1"&gt;-- 3&lt;/span&gt;

&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;   &lt;span class="c1"&gt;-- 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;(.)&lt;/code&gt; and &lt;code&gt;($)&lt;/code&gt; are different things. More can be read about that here - &lt;a href="http://learnyouahaskell.com/higher-order-functions#function-application"&gt;Learn you a haskell ($)&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>haskell</category>
      <category>functional</category>
      <category>programming</category>
    </item>
    <item>
      <title>ELI5 - "Map-filter-reduce" </title>
      <dc:creator>Namc</dc:creator>
      <pubDate>Mon, 04 Dec 2017 10:12:14 +0000</pubDate>
      <link>https://dev.to/__namc/eli5---map-filter-reduce-40p</link>
      <guid>https://dev.to/__namc/eli5---map-filter-reduce-40p</guid>
      <description>&lt;p&gt;Many developers that I have lately interacted with associate map-reduce-filter with functional programming, and they are actually dreaded by the combination. It’s one of the reasons they avoid using functional languages, because &lt;strong&gt;god forbid they would have to learn how to use map, reduce and filter&lt;/strong&gt; — 3 functions present in Ruby, Python, Javascript, PHP, Erlang, Scala and many others languages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So what’s the big deal with map-reduce-filter anyway?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;They are just higher-order functions used in functional programming. And they work on anything that is iterable (list, tuple, sets, dicts, and even strings).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s consider a problem statement to be able to follow more clearly. (We’re using Python for the ease of purpose, but I’ll add a gist of how the same thing can be done in various other languages)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem statement : &lt;br&gt;
We need the sum of all even squares of integers in the range [0, 9]&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Map (Transform)
&lt;/h3&gt;

&lt;p&gt;This function takes a unary function and a list. Unary function is a function that takes one variable. The resultant for map function is a same-sized list of transformed values. The transformation of value is subject to the definition of unary function with the parameter. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use when&lt;/strong&gt; : You want to transform all elements in a set to another set of values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parameters&lt;/strong&gt; : unary transformation function &amp;amp; array to be transformed&lt;/p&gt;

&lt;p&gt;In order to solve the problem statement, let’s say, our first step is to map the function of calculating squares over the range of numbers [0, 9]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; nums &lt;span class="o"&gt;=&lt;/span&gt; map&lt;span class="o"&gt;(&lt;/span&gt;lambda x : x&lt;span class="k"&gt;*&lt;/span&gt;x, &lt;span class="o"&gt;[&lt;/span&gt;i &lt;span class="k"&gt;for &lt;/span&gt;i &lt;span class="k"&gt;in &lt;/span&gt;range&lt;span class="o"&gt;(&lt;/span&gt;10&lt;span class="o"&gt;)])&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; nums
&lt;span class="o"&gt;[&lt;/span&gt;0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lambdas are often to define map functions. You can use regular functions as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Filter 
&lt;/h3&gt;

&lt;p&gt;This function takes a predicate, (a unary function which returns a bool) and the set of values; and gives a resultant set with only those set of values for which the predicate returns true. The resultant set may or may not be of the same length. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use when&lt;/strong&gt; : You want to remove elements from a set based on a condition. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parameters&lt;/strong&gt; : unary predicate (should only return a bool) &amp;amp; array to be filtered. &lt;/p&gt;

&lt;p&gt;In context to our problem above, let’s try to remove all the odd squares from the list. For that, let our predicate return true if the element is even.&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; def is_even&lt;span class="o"&gt;(&lt;/span&gt;x&lt;span class="o"&gt;)&lt;/span&gt;:
        &lt;span class="k"&gt;return &lt;/span&gt;x % 2 &lt;span class="o"&gt;==&lt;/span&gt; 0
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; evens &lt;span class="o"&gt;=&lt;/span&gt; filter&lt;span class="o"&gt;(&lt;/span&gt;is_even, nums&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; evens
&lt;span class="o"&gt;[&lt;/span&gt;0, 4, 16, 36, 64]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Comprehensions often make it easier to write filters. Those can be used in place of functions as well.&lt;/p&gt;
&lt;h3&gt;
  
  
  Reduce (Accumulate)
&lt;/h3&gt;

&lt;p&gt;This function takes a binary function and a set of values, and it produces a single value. It does so by running the binary function over the set of values, and accumulating them into a single value. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use when&lt;/strong&gt; : You want an accumulated or concatenated value using given set of values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parameters&lt;/strong&gt; : binary function and set of values.&lt;/p&gt;

&lt;p&gt;In context to our problem above, we shall add up all the even-valued squares.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; reduce&lt;span class="o"&gt;(&lt;/span&gt;lambda x,y : x+y, evens&lt;span class="o"&gt;)&lt;/span&gt;
120
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we look at the defintion of reduce in functools module,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;alist&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;alist&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="p"&gt;[]:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

    &lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;alist&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;alist&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;
        &lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we see that reduce function starts by setting answer as the first element of array. Then it traverses the array from left to right, invoking a callback function on each element. The cumulative value is passed from callback to callback. After all elements are traversed, cumulative value is returned. &lt;/p&gt;

&lt;h3&gt;
  
  
  Combining map-filter-reduce
&lt;/h3&gt;

&lt;p&gt;We saw a lot of small functions being written above, that was just for understanding. The true power of higher-order-functions lie in combination. So our problem statement can be solved in one line of code 😅&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; reduce&lt;span class="o"&gt;(&lt;/span&gt;lambda a, b: a + b, map&lt;span class="o"&gt;(&lt;/span&gt;lambda x: x &lt;span class="k"&gt;*&lt;/span&gt; x, filter&lt;span class="o"&gt;(&lt;/span&gt;lambda y: not y % 2, range&lt;span class="o"&gt;(&lt;/span&gt;10&lt;span class="o"&gt;))))&lt;/span&gt;
120
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don’t Panic! ! We can break it down for simplicity.&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;f_add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="n"&gt;f_square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
&lt;span class="n"&gt;f_is_even&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now the complex expression can be rewritten as&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; reduce&lt;span class="o"&gt;(&lt;/span&gt;f_add , map&lt;span class="o"&gt;(&lt;/span&gt;f_square , filter&lt;span class="o"&gt;(&lt;/span&gt;f_is_even, range[0, 10]&lt;span class="o"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;As a sidenote, there is no restriction on using map , filter &amp;amp; reduce together. You can make your own combinations, and order your functions as per the need.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Other languages 
&lt;/h3&gt;

&lt;p&gt;Keeping the problem statement same as above, check out how map-reduce-filter can be used in other programming languages, and is not a rocket science at all 🚀&lt;/p&gt;
&lt;h4&gt;
  
  
  Ruby
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="n"&gt;ans&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;              &lt;span class="c1"&gt;# map operation (x*x) over nums&lt;/span&gt;
              &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;      &lt;span class="c1"&gt;# select event elements&lt;/span&gt;
              &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;        &lt;span class="c1"&gt;# reduce the list by `+` operation&lt;/span&gt;

    &lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;                                 
    &lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;ans&lt;/span&gt;                                  

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Clojure
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;reduce&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; 
        &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; 
             &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;even?&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Javascript
&lt;/h4&gt;

&lt;p&gt;(I’m really bad at this 😕)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nx"&gt;ans&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; 
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try it out in other languages as well!&lt;/p&gt;

</description>
      <category>explainlikeimfive</category>
      <category>programming</category>
      <category>beginners</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Applicative functors in Haskell</title>
      <dc:creator>Namc</dc:creator>
      <pubDate>Sat, 02 Dec 2017 11:39:36 +0000</pubDate>
      <link>https://dev.to/__namc/applicative-functors-inhaskell-gm</link>
      <guid>https://dev.to/__namc/applicative-functors-inhaskell-gm</guid>
      <description>&lt;h3&gt;
  
  
  Functor
&lt;/h3&gt;

&lt;p&gt;According to &lt;a href="https://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Functor.html"&gt;Haskell&lt;/a&gt;, a functor is simply something that can be mapped over. In other words, it is an abstraction for a context with the ability to apply a function to all the things inside the context. &lt;br&gt;
The context can be defined as container, computation etc. &lt;br&gt;
E.g.; a list or sequence is a container of homogenous elements. You can apply a function to each of the elements, which produces a new sequence of elements transformed by the function.&lt;/p&gt;

&lt;p&gt;Let’s create a new functor, implementing &lt;code&gt;myfmap&lt;/code&gt; such that &lt;code&gt;Data.Map&lt;/code&gt; is an instance of the new functor typeclass. &lt;code&gt;myfmap&lt;/code&gt; applies a function &lt;code&gt;f&lt;/code&gt; to the value(s) inside functor’s context while preserving the context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Data.Map&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;DataMap&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Data.List&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;DataList&lt;/span&gt;

&lt;span class="kr"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;MyFunctor&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
  &lt;span class="n"&gt;myfmap&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;

&lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Ord&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;MyFunctor&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;DataMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Map&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
  &lt;span class="n"&gt;myfmap&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;DataMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fromList&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; 
                     &lt;span class="kt"&gt;DataList&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; 
                     &lt;span class="kt"&gt;DataMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;toList&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Applicative Functor
&lt;/h3&gt;

&lt;p&gt;In computer science, applicative is an abstraction for a context, and it has the ability to apply functions in the same type of context to all elements in the context. For example, a sequence A which has homogenous elements, and sequence B which consists of functions that can be applied to sequence A , which produce a new sequence of elements transformed by all the functions. (Super confusing? Sorry 😕)&lt;/p&gt;

&lt;p&gt;The Applicative typeclass in Haskell is located in Control.Applicative module and defines &lt;code&gt;pure&lt;/code&gt; and &lt;code&gt;(&amp;lt;*&amp;gt;)&lt;/code&gt; .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="kr"&gt;class&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Functor&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Applicative&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
    &lt;span class="n"&gt;pure&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;*&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;An Applicative has to be Functor. This is a class constraint.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;pure&lt;/code&gt; takes any context and returns an Applicative the value of context inside it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;(&amp;lt;*&amp;gt;)&lt;/code&gt; is a representation of &lt;code&gt;fmap&lt;/code&gt; , where &lt;code&gt;&amp;lt;*&amp;gt;&lt;/code&gt; takes a functor that has a function in it and another functor and run the function from first functor and maps it over the second functor. Whereas &lt;code&gt;fmap&lt;/code&gt; which takes a function and functor and applies the function inside the functor.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;h3&gt;
  
  
  List an an Applicative!
&lt;/h3&gt;

&lt;p&gt;List also instantiates an applicative typeclass, with implementation as :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="kt"&gt;Applicative&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
    &lt;span class="n"&gt;pure&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;xs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;xs&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the implementation of &lt;code&gt;(&amp;lt;*&amp;gt;)&lt;/code&gt; is basically a list comprehension, where every function is applied to every value. For example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ghci&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &amp;lt;&lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;2, 3] &amp;lt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;4, 5]
&lt;span class="o"&gt;[&lt;/span&gt;8, 10, 12, 15]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to apply each function in first list to the respective value in second list, the ZipList typeclass is very handy.&lt;/p&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;h3&gt;
  
  
  Curious case of &lt;code&gt;Either&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The base Monad instance for Either is defined as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="kt"&gt;Monad&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Either&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
  &lt;span class="n"&gt;return&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Right&lt;/span&gt;
  &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;  &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
  &lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This instance has inherent &lt;code&gt;short-circuiting&lt;/code&gt;, where we go from left to right and once a single computation fails into the Left , then all do as well. But in case you would like to collect error messages which occur anywhere in the above computation, it goes against &lt;code&gt;(&amp;gt;&amp;gt;=)&lt;/code&gt; and &lt;code&gt;lazy-evaluation/short-circuiting&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;Lazy evaluation is when we proceed from left to right, when a single computation “fails” into the Left then all the rest do as well.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(&amp;gt;&amp;gt;=)&lt;/code&gt; takes a function, maps it over an instance of a monad and then flattens the result.&lt;br&gt;
&lt;code&gt;(&amp;gt;&amp;gt;=) :: m a -&amp;gt; (a -&amp;gt; m b) -&amp;gt; m b&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(&amp;gt;&amp;gt;=)&lt;/code&gt; produces &lt;code&gt;m b&lt;/code&gt; from &lt;code&gt;m a&lt;/code&gt; so long as it can run &lt;code&gt;(a -&amp;gt; m b)&lt;/code&gt; . This demands that the value of a should ideally exists during the time of computation, and this is impossible for Either .&lt;/p&gt;

&lt;p&gt;—&lt;/p&gt;

&lt;p&gt;So let’s try to solve the above problem by defining a &lt;strong&gt;Functor instance of Either&lt;/strong&gt; .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="kt"&gt;Functor&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Either&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
    &lt;span class="n"&gt;fmap&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
    &lt;span class="n"&gt;fmap&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Things we can understand from the above definition :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We know the definition of &lt;code&gt;fmap :: (c -&amp;gt; d) -&amp;gt; f c -&amp;gt; f d&lt;/code&gt; .&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If we replace f with Either a , we get &lt;br&gt;
&lt;code&gt;fmap :: (c -&amp;gt; d) -&amp;gt; Either a c -&amp;gt; Either a d&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The problem with this implementation of Either is that we cannot map over Left .&lt;br&gt;
(😱Why?) To understand that, let &lt;code&gt;Either a b&lt;/code&gt; computation, which may succeed and return b or fail with error a , similar to monad instance. So the functor instance does not map over Left values since you would want to map over the computation, if it fails, there is nothing to manipulate.&lt;/p&gt;

&lt;p&gt;—&lt;br&gt;
Let’s see if we can implement an &lt;code&gt;Applicative instance&lt;/code&gt; instead, which cannot have a corresponding Monad , to solve the problem.&lt;/p&gt;

&lt;p&gt;As we saw the definition of Applicative, we will define &lt;code&gt;pure&lt;/code&gt; and &lt;code&gt;(&amp;lt;*&amp;gt;)&lt;/code&gt; . &lt;/p&gt;

&lt;p&gt;Defining pure is rather simple, as we want it return the Right element. Implementation of &lt;code&gt;(&amp;lt;*&amp;gt;)&lt;/code&gt; is rather tricky . The following cases need to be considered for defining &lt;code&gt;(&amp;lt;*&amp;gt;)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="kt"&gt;Applicative&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Either&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
    &lt;span class="n"&gt;pure&lt;/span&gt;                 &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="kt"&gt;Right&lt;/span&gt;
    &lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;*&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    
    &lt;span class="kt"&gt;Left&lt;/span&gt;  &lt;span class="n"&gt;e&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;*&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="kr"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;          
    &lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="kr"&gt;_&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;*&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Left&lt;/span&gt;  &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;          
    &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="n"&gt;e1&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;*&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first statement is the pure statement.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;(&amp;lt;*&amp;gt;)&lt;/code&gt; allows evaluation in parallel instead of necessarily needing results from previous computation to compute present values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thus, we can use our purely Applicative Either to collect errors, ignoring Right if any Left exist in the sequence.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As soon as it hits a Left, it aborts and returns that Left.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
ghci&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;++&lt;span class="o"&gt;)&lt;/span&gt; &amp;lt;&lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt; Left &lt;span class="s2"&gt;"Hello"&lt;/span&gt; &amp;lt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; undefined 
Left &lt;span class="s2"&gt;"Hello"&lt;/span&gt;                              &lt;span class="nt"&gt;--&lt;/span&gt; not undefined

ghci&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;++&lt;span class="o"&gt;)&lt;/span&gt; &amp;lt;&lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt; Right &lt;span class="s2"&gt;"Hello"&lt;/span&gt; &amp;lt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; undefined 
&lt;span class="k"&gt;***&lt;/span&gt; Exception: Prelude.undefined          &lt;span class="nt"&gt;--&lt;/span&gt; undefined

ghci&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;++&lt;span class="o"&gt;)&lt;/span&gt;  &amp;lt;&lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt; Right &lt;span class="s2"&gt;"Hello"&lt;/span&gt; &amp;lt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Left &lt;span class="s2"&gt;" World"&lt;/span&gt;
Left &lt;span class="s2"&gt;" World"&lt;/span&gt;

ghci&amp;gt; &lt;span class="o"&gt;(&lt;/span&gt;++&lt;span class="o"&gt;)&lt;/span&gt;  &amp;lt;&lt;span class="nv"&gt;$&amp;gt;&lt;/span&gt; Right &lt;span class="s2"&gt;"Hello"&lt;/span&gt; &amp;lt;&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Right &lt;span class="s2"&gt;" World"&lt;/span&gt;
Right &lt;span class="s2"&gt;"Hello World"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;—&lt;/p&gt;

&lt;h3&gt;
  
  
  Limitations
&lt;/h3&gt;

&lt;p&gt;There’s some limitations to using purely applicative functor. As we saw in the definition of &lt;code&gt;(&amp;gt;&amp;gt;=) :: m a -&amp;gt; (a -&amp;gt; m b) -&amp;gt; m b&lt;/code&gt; ; which means that without &lt;code&gt;(&amp;gt;&amp;gt;=)&lt;/code&gt; you can’t pick “what to do next based on what came before”.&lt;br&gt;
Also, if you take a generally pure function and feed the applicative arguments to it, like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; 
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;$&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;getLine&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;getProcessID&lt;/span&gt; &lt;span class="s"&gt;"chrome"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;*&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;getFreeMemory&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All the arguments will get evaluated, no matter what. That is, you cannot express — “if the second argument exits, abort the rest of the computation”. Due to this, anything recursive, as well as most interactive programs do not use Applicatives. Monads, on the other hand are a good choice in that case.&lt;/p&gt;

&lt;p&gt;Original Article - &lt;a href="https://medium.com/lazy-eval/applicative-functors-in-haskell-f509e1c764d3"&gt;Applicative functors in Haskell&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;—&lt;br&gt;
More Resources :&lt;/p&gt;

&lt;p&gt;&lt;a href="http://learnyouahaskell.com/functors-applicative-functors-and-monoids"&gt;http://learnyouahaskell.com/functors-applicative-functors-and-monoids&lt;/a&gt;&lt;br&gt;
&lt;a href="http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html"&gt;http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://hackage.haskell.org/package/base-4.8.1.0/docs/Control-Applicative.html"&gt;https://hackage.haskell.org/package/base-4.8.1.0/docs/Control-Applicative.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/17409260/what-advantage-does-monad-give-us-over-an-applicative"&gt;https://stackoverflow.com/questions/17409260/what-advantage-does-monad-give-us-over-an-applicative&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>haskell</category>
      <category>functional</category>
      <category>programming</category>
    </item>
    <item>
      <title>lazy-seq in Clojure</title>
      <dc:creator>Namc</dc:creator>
      <pubDate>Tue, 28 Nov 2017 04:20:02 +0000</pubDate>
      <link>https://dev.to/__namc/lazy-seq-inclojure-bmh</link>
      <guid>https://dev.to/__namc/lazy-seq-inclojure-bmh</guid>
      <description>&lt;p&gt;Basic idea : &lt;br&gt;
&lt;strong&gt;Evaluation of an expression is delayed until the value is needed.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are two ways to achieve the above :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use lambda functions at runtime&lt;/li&gt;
&lt;li&gt;use macros/special forms at compile time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With lazy eval techniques, it is possible to construct infinite data structures that are evaluated as consumed, using lambdas, closures and recursion. In clojure, they are generated using &lt;code&gt;lazy-seq&lt;/code&gt; and &lt;code&gt;cons&lt;/code&gt; forms.&lt;/p&gt;

&lt;p&gt;Let’s take an example from the &lt;a href="http://clojuredocs.org/clojure.core/lazy-seq"&gt;Clojure docs for lazy-seq&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;user&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;lazy-cat&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;rest&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c1"&gt;;; =&amp;gt; #'user/fib-seq&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;lazy-cat&lt;/code&gt; is another macro to concatenate lazy sequences. We shall write the above expression to an alternate translation which uses &lt;code&gt;lazy-seq&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;user&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;concat&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;lazy-seq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;lazy-seq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;rest&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c1"&gt;;; =&amp;gt; #'user/fib-seq&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;user&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;doall&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;take&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;lazy-seq&lt;/code&gt; evaluates for very large indexes without giving an OutOfMemoryException/StackOverFlowError. (Might throw ArithmeticException for extremely large integer, though)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How this works internally :&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;lazy-seq&lt;/code&gt; executes the body once the first time it is accessed.&lt;/li&gt;
&lt;li&gt;the result is cached and used whenever the function is called in future.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Going back to the example;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[0 1]&lt;/code&gt; is the base case for the problem. In Fibonacci’s sequence, the first two values are 0 and 1. The consecutive computation of each value requires summation of previous value and the value before that one. Hence, we need at least two values to start the process.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(map + (rest fib-seq) fib-seq) &lt;/code&gt;&lt;br&gt;
Here, rest will return the part of &lt;code&gt;fib-seq&lt;/code&gt; without it’s head. &lt;code&gt;map&lt;/code&gt; will combine two sequences using &lt;code&gt;+&lt;/code&gt; and produce a next sequence. Let’s take the fibonacci sequence &lt;code&gt;fib-seq-temp&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;user&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq-temp&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;user&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;rest&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq-temp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq-temp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c1"&gt;;; =&amp;gt; (1 2 3 5)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sequences fed to map are part of same basic sequence . The first sequence &lt;code&gt;(rest [seq])&lt;/code&gt; is the base seq without the head. The second sequence is the base seq, including the first value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the base sequence here?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;According to fibonacci number’s definition, &lt;code&gt;A(n) = A(n-1) + A(n-2)&lt;/code&gt; . We need to be able to access previously computed values in order to extend the sequence. So, we use the recursive reference to &lt;code&gt;fib-seq&lt;/code&gt; to access our previous results. The base seq is typically &lt;code&gt;[0 1]&lt;/code&gt; here to begin with.&lt;/p&gt;

&lt;p&gt;Let’s follow the steps mentioned above and run through the process :&lt;/p&gt;

&lt;p&gt;Take the first element of &lt;code&gt;fib-seq&lt;/code&gt; (base case [0 1] ) — 0&lt;br&gt;
Take the second element of &lt;code&gt;fib-seq&lt;/code&gt; — 1&lt;br&gt;
Take the third element of &lt;code&gt;fib-seq&lt;/code&gt; — Stuck, aren’t we? &lt;/p&gt;

&lt;p&gt;Here, we use map to generate a sequence which is used as remaining values.&lt;br&gt;
The sum of rest [0 1] which is 1, and first item of [0 1] , which is zero is 1 ; which is our result below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;user&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;rest&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c1"&gt;;; =&amp;gt; (1)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, let’s repeat the process for generating fourth element.&lt;br&gt;
We use &lt;code&gt;map&lt;/code&gt; again for generating base seq. &lt;br&gt;
Compute the sum of second item of &lt;code&gt;rest [0 1]&lt;/code&gt;, whose value is (1) as computed above; and second item of &lt;code&gt;[0 1]&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;user&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;rest&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c1"&gt;;; =&amp;gt; (2)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Easy so far?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now generate the fourth element. &lt;br&gt;
Sum the third element of &lt;code&gt;rest [0 1]&lt;/code&gt; (which we computed as 1) and fourth item generated from &lt;code&gt;[0 1]&lt;/code&gt; (which was 2 ). And we get 3 .&lt;/p&gt;

&lt;p&gt;This will keep building up to match the desired definition for series. To understand this better, let’s add a print statement to our original definition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;user&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;lazy-cat&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; 
            &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="w"&gt; 
              &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"%d + %d"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;rest&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c1"&gt;;; =&amp;gt; #'user/fib-seq&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;user&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;doall&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;take&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fib-seq&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c1"&gt;;; =&amp;gt; (0 1 1 2 3 5 8 13 21 34)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;—-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Working with different data-types&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;map&lt;/code&gt; , &lt;code&gt;reduce&lt;/code&gt; , &lt;code&gt;take&lt;/code&gt; etc work in terms of &lt;code&gt;first&lt;/code&gt; , &lt;code&gt;rest&lt;/code&gt; and &lt;code&gt;more&lt;/code&gt; .&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;first&lt;/code&gt; (as well as others) ensure if the collection implements &lt;code&gt;ISeq&lt;/code&gt; .&lt;/li&gt;
&lt;li&gt;If yes, then these functions are implemented directly. Else, a &lt;code&gt;seq&lt;/code&gt; view of the collection is created to implement &lt;code&gt;first&lt;/code&gt; / &lt;code&gt;more&lt;/code&gt; / &lt;code&gt;rest&lt;/code&gt; .&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;lazy-seq&lt;/code&gt; &lt;strong&gt;macro&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The macro uses &lt;a href="https://en.wikipedia.org/wiki/Thunk"&gt;thunk&lt;/a&gt; to store the sequence-generating calculations.&lt;/li&gt;
&lt;li&gt;When an element/chunk of sequence is requested, next thunk is called to retrieve the values.&lt;/li&gt;
&lt;li&gt;The thunk creates next subroutine to represent the tail of the sequence (&lt;a href="https://github.com/danielmiladinov/joy-of-clojure/blob/master/src/joy-of-clojure/chapter6/laziness.clj"&gt;lose your head&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;These thunks implement sequence interface, each thunk is called just once, and then cached. So the &lt;a href="https://clojuredocs.org/clojure.core/realized_q"&gt;realized portion&lt;/a&gt; is just a sequence of values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Holding onto the head&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Take these two methods to generate infinite lazy seqs of randoms.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;user&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;infinite&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;lazy-seq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;cons&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;infinite&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c1"&gt;;; =&amp;gt; #'user/infinite&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;--&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;user&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;infinite-1&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;lazy-seq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;cons&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;infinite-1&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="c1"&gt;;; =&amp;gt; #'user/infinite-1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;user&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;zyx&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;infinite-1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you try to run &lt;code&gt;(last (infinite))&lt;/code&gt; ; it won’t crash, but will neither terminate. However, &lt;code&gt;(last zyx)&lt;/code&gt; will crash quickly with an &lt;code&gt;OutOfMemoryError&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;This is because of “holding onto the head”.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;(last (infinite))&lt;/code&gt; , Clojure disposes the earlier members of sequence because it is able to determine that they are not of any use. Hence, memory is not lost. Whereas, in &lt;code&gt;(last zyx)&lt;/code&gt; , the program is holding on to the definition , and hence, it is not able to clear the memory. This causes it to throw OutOfMemoryError .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So what’s the point of &lt;code&gt;lazy-seq&lt;/code&gt; ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;lazy-seq&lt;/code&gt; is just one of many possible ways to create lazy sequences. And there are &lt;a href="http://clojure-doc.org/articles/language/laziness.html#commonly-used-functions-that-produce-lazy-sequences"&gt;several other ways&lt;/a&gt; to do it in clojure.&lt;/p&gt;

&lt;p&gt;If a sequence is not lazy, it often holds onto it’s head, which consumes a lot of heap space. If it is lazy, it is computed, then discarded as it is not used for further computations. This is particularly useful when you are dealing with huge sequences, and only using a small part of them for sequential computation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/lazy-eval/lazy-seq-in-clojure-da06f6d35971"&gt;Original Article on Medium&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/lazy-eval"&gt;More on Lazy Eval&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;—&lt;br&gt;
More references :&lt;br&gt;
&lt;a href="https://purelyfunctional.tv/lesson/what-are-lazy-sequences/"&gt;https://purelyfunctional.tv/lesson/what-are-lazy-sequences/&lt;/a&gt;&lt;br&gt;
&lt;a href="http://theatticlight.net/posts/Lazy-Sequences-in-Clojure/"&gt;http://theatticlight.net/posts/Lazy-Sequences-in-Clojure/&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.thesoftwaresimpleton.com/blog/2014/09/08/lazy-seq/"&gt;http://www.thesoftwaresimpleton.com/blog/2014/09/08/lazy-seq/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>clojure</category>
      <category>programming</category>
      <category>functional</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
