<?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: Shalveena</title>
    <description>The latest articles on DEV Community by Shalveena (@shalveena).</description>
    <link>https://dev.to/shalveena</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%2F831818%2F56153dce-0576-4d3c-9a8b-0bd326040280.png</url>
      <title>DEV Community: Shalveena</title>
      <link>https://dev.to/shalveena</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shalveena"/>
    <language>en</language>
    <item>
      <title>What are Lambdas and Higher Order Functions in Kotlin and How Can You Use Them?</title>
      <dc:creator>Shalveena</dc:creator>
      <pubDate>Wed, 14 Sep 2022 06:07:09 +0000</pubDate>
      <link>https://dev.to/shalveena/what-are-lambdas-and-higher-order-functions-in-kotlin-and-how-can-you-use-them-2ieh</link>
      <guid>https://dev.to/shalveena/what-are-lambdas-and-higher-order-functions-in-kotlin-and-how-can-you-use-them-2ieh</guid>
      <description>&lt;p&gt;Lambdas are widely used in Kotlin so you've most likely come across them, but if you're like me you might be asking, "What exactly is a Lambda? What does it do and how can I use them?"&lt;/p&gt;

&lt;p&gt;This article attempts to answer those questions. Specifically:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What a lambda is&lt;/li&gt;
&lt;li&gt;How to write a lambda (in other words, the syntax of a lambda)&lt;/li&gt;
&lt;li&gt;What a higher order function is and how lambdas are used in higher order functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  So, what is a lamda?
&lt;/h2&gt;

&lt;p&gt;The simple answer is that lambdas are functions. But more specifically, a lambda is &lt;em&gt;a function&lt;/em&gt; that is &lt;em&gt;written as an expression&lt;/em&gt;, and that is &lt;em&gt;not declared using the fun keyword&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Here is an example of a regular function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&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;The same function can be written as a lambda:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;greeting&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can call the lambda function by using the variable name followed by parentheses: &lt;code&gt;greeting()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;greeting&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// In the main function:&lt;/span&gt;
&lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// prints: Hello World!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: you can also call the lambda function by using &lt;code&gt;greeting.invoke()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Great, now that you know a bit about what a lambda is, we will have a look at how to write (and read) a lambda in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to write a lambda? The syntax.
&lt;/h2&gt;

&lt;p&gt;You can write a lambda within curly braces. The parameters are separated from the function body by an arrow. So the parameters are written first, then an arrow and then the function body.&lt;/p&gt;

&lt;p&gt;Extending our &lt;code&gt;greeting&lt;/code&gt; example, let's give it one parameter so you can see the syntax of a lambda with parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;greeting&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, $name"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;

&lt;span class="c1"&gt;// In the main function:&lt;/span&gt;
&lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sam"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// prints: Hello, Sam&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Function Types
&lt;/h3&gt;

&lt;p&gt;Since Kotlin is a statically typed language, every variable must have a type. In the above example, the &lt;code&gt;greeting&lt;/code&gt; variable is of a &lt;em&gt;function type&lt;/em&gt; because it holds a function (since a lambda is a function after all). Variables that hold lambdas are of a &lt;em&gt;function type&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Kotlin can infer the function type implicitly, but you can also write it explicitly.&lt;/p&gt;

&lt;p&gt;If you were to write out the full syntax of &lt;code&gt;greeting&lt;/code&gt;, it would look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, $name"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above reads as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;greeting&lt;/code&gt; is a variable of a function type&lt;/li&gt;
&lt;li&gt;it will hold a function&lt;/li&gt;
&lt;li&gt;the function it holds will take a String argument and return Unit.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Short-hand syntax for lambdas
&lt;/h3&gt;

&lt;p&gt;There are three simplifications we can make to the lambda syntax, which makes it easier to read.&lt;/p&gt;

&lt;p&gt;1) When the variable declaration has an explicitly declared function type, for example a function type that takes a String and returns Unit (like in the example above), you don't have to explicitly define the parameter of the lambda as being of String type. &lt;br&gt;
This is because Kotlin already knows that the lambda will have a String type parameter since it has been explicitly defined in the variable declaration. This is easier to illustrate through code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, $name"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;

&lt;span class="cm"&gt;/* We don't need to explicitly say that the
name parameter will be of String type.
We can simplify it to:
*/&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, $name"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) If the lambda has only one parameter, you don't have to write the parameter. Instead, there is an &lt;code&gt;it&lt;/code&gt; keyword that you can use to refer to the parameter. So you can further simplify &lt;code&gt;greeting&lt;/code&gt; to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello! $it"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) You don't need to use the &lt;code&gt;return&lt;/code&gt; keyword. This is because the last expression within the lambda's body is treated as the return value. To illustrate, let's change our &lt;code&gt;greeting&lt;/code&gt; function to return a String:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;String&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, $it"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sam"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Will return "Hello, Sam" even though the return keyword was not used in greeting.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is a higher order function?
&lt;/h2&gt;

&lt;p&gt;Higher order functions are functions and take a function as an argument. Phew, so many "functions" there!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; When writing higher order functions, it is best practice in Kotlin to have the function argument as the last parameter.&lt;/p&gt;

&lt;p&gt;An example of a higher order function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;createLongGreeting&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstSentence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secondSentence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"${operation(firstSentence)} $secondSentence"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, &lt;code&gt;createLongGreeting&lt;/code&gt; is a higher order function that creates and returns a string template using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the result of calling an operation (function) on the first sentence, and&lt;/li&gt;
&lt;li&gt;the second sentence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It takes 3 arguments:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A string (the &lt;code&gt;firstSentence&lt;/code&gt; parameter)&lt;/li&gt;
&lt;li&gt;A string (the &lt;code&gt;secondSentence&lt;/code&gt; parameter)&lt;/li&gt;
&lt;li&gt;A function (the &lt;code&gt;operation&lt;/code&gt; parameter)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Okay, so how can lambdas help you when it comes to higher order functions?
&lt;/h2&gt;

&lt;p&gt;Since higher order functions take another function as an argument, you need to be able to pass functions to them. If you try to pass a regular function as the argument, Kotlin will throw an error because it will think you're trying to call the function instead of simply passing it in as an argument. For example if you try the following, it will give you an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;partGreeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"${name}! It's has been a long time!"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;createLongGreeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sam"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"How are you?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;partGreeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// error: Function invocation 'partGreeting(...)' expected. No value passed for parameter 'name'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need a way to easily pass functions as arguments to the higher order functions. And that's where lambdas come to the rescue.&lt;/p&gt;

&lt;h3&gt;
  
  
  Passing in a lambda as a parameter to a higher order function
&lt;/h3&gt;

&lt;p&gt;Lambdas allow you to easily pass functions as arguments.&lt;/p&gt;

&lt;p&gt;Continuing with the example from above, you can pass a lambda directly into &lt;code&gt;createLongGreeting&lt;/code&gt; as its last argument. The lambda must be one that takes a String as it's argument and returns a String.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The createLongGreeting function&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;createLongGreeting&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstSentence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secondSentence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"${operation(firstSentence)} $secondSentence"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// In the main function:&lt;/span&gt;
&lt;span class="c1"&gt;// Calling createLongGreeting and passing a lambda directly to it as it's last argument.&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;firstGreeting&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createLongGreeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sam"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"How are you?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"$name! It's been a long time!"&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstGreeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Sam! It's been a long time! How are you?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a breakdown of what is happening in the above example:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We call &lt;code&gt;createLongGreeting&lt;/code&gt;, passing it:

&lt;ul&gt;
&lt;li&gt;"Sam" as it's first argument&lt;/li&gt;
&lt;li&gt;"How are you?" as it's second argument&lt;/li&gt;
&lt;li&gt;A lambda as it's third argument&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;createLongGreeting&lt;/code&gt; calls our lambda, passing it "Sam"&lt;/li&gt;
&lt;li&gt;Our lambda returns a string: "Sam! It's been a longtime!"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;createLongGreeting&lt;/code&gt; creates a string template, inserting the string returned from the lambda call ("Sam! It's been a longtime!") and the string we passed as the second argument ("How are you?")&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;createLongGreeting&lt;/code&gt; returns the string it created in step 4: "Sam! It's been a long time! How are you?"&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Trailing lambda syntax
&lt;/h3&gt;

&lt;p&gt;To make our example easier to read, we can actually put the lambda outside the parenthesis:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;firstGreeting&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createLongGreeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sam"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"How are you?"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"$name! It's been a long time!"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is known as a trailing lambda syntax.&lt;/p&gt;

&lt;h3&gt;
  
  
  Assigning a lambda to a variable and passing that variable to the higher order function
&lt;/h3&gt;

&lt;p&gt;Another way we can use lambdas with higher order functions is by assigning the lambda to a variable and passing that variable as an argument to the higher function.&lt;/p&gt;

&lt;p&gt;First, we need to make a lambda and assign it to a variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;String&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, $it!"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we pass &lt;code&gt;greeting&lt;/code&gt; to &lt;code&gt;createLongGreeting&lt;/code&gt;. Note that we don't call &lt;code&gt;greeting&lt;/code&gt; directly. We just &lt;strong&gt;pass&lt;/strong&gt; it to &lt;code&gt;createLongGreeting&lt;/code&gt;. It's &lt;code&gt;createLongGreeting&lt;/code&gt; that calls &lt;code&gt;greeting&lt;/code&gt; when making its string template.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The createLongGreeting function:&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;createLongGreeting&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstSentence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secondSentence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"${operation(firstSentence)} $secondSentence"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Our lambda - the greeting function:&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;String&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, $it!"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// In the main function, call createLongGreeting and pass it greeting as it's last argument:&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;secondGreeting&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createLongGreeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sam"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"How are you?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secondGreeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Hello, Sam! How are you?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As mentioned earlier, if &lt;code&gt;greeting&lt;/code&gt; were a regular function (or a single line function) instead of a lambda, we could not pass it to &lt;code&gt;createLongGreeting&lt;/code&gt; as above, because Kotlin will think that we are trying to call greeting and give an error. But there is a way to pass a regular function as an argument: we can use the &lt;code&gt;::&lt;/code&gt; symbol to pass the function as a reference.&lt;/p&gt;

&lt;p&gt;In the example below, &lt;code&gt;greeting&lt;/code&gt; is written as a single line function and passsed to &lt;code&gt;createLongGreeting&lt;/code&gt; as a reference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&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, $name!"&lt;/span&gt;

&lt;span class="c1"&gt;// In the main function:&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;secondGreeting&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createLongGreeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sam"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"How are you?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Bonus tip: you could capture a user's input, clean it up and use it in &lt;code&gt;createLongGreeting&lt;/code&gt;:
&lt;/h3&gt;

&lt;p&gt;First, create a function to clean the user's input data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;cleanUserInput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;userInput&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;userInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userInput&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="n"&gt;userInput&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="nf"&gt;uppercaseChar&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nf"&gt;trim&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;Then, in the main function, prompt the user to enter their name and assign it to a variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Please enter your name: "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;usersName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;readln&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, call &lt;code&gt;createLongGreeting&lt;/code&gt;, passing it the input from the user, the string "Good to see you!" and the &lt;code&gt;cleanUserInput&lt;/code&gt; function. The &lt;code&gt;cleanedUserInput&lt;/code&gt; function will be called within &lt;code&gt;createLongGreeting&lt;/code&gt; to clean the input from the user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;thirdGreeting&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createLongGreeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usersName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Good to see you!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cleanUserInput&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thirdGreeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// if the user inputs "hallie", the output would be "Hallie Good to see you!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So there you have it - now you know more about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what lambdas are,&lt;/li&gt;
&lt;li&gt;how they look like,&lt;/li&gt;
&lt;li&gt;how to write them,&lt;/li&gt;
&lt;li&gt;what higher order functions are,&lt;/li&gt;
&lt;li&gt;and how you can use a lambda to pass a function as an argument to a higher order function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>lambda</category>
    </item>
    <item>
      <title>What the heck is an API? A Beginner's guide to APIs</title>
      <dc:creator>Shalveena</dc:creator>
      <pubDate>Wed, 23 Mar 2022 23:52:49 +0000</pubDate>
      <link>https://dev.to/shalveena/what-the-heck-is-an-api-a-beginners-guide-to-apis-4k0c</link>
      <guid>https://dev.to/shalveena/what-the-heck-is-an-api-a-beginners-guide-to-apis-4k0c</guid>
      <description>&lt;h2&gt;
  
  
  What is an API?
&lt;/h2&gt;

&lt;p&gt;API is short for &lt;strong&gt;A&lt;/strong&gt;pplication &lt;strong&gt;P&lt;/strong&gt;rogramming &lt;strong&gt;I&lt;/strong&gt;nterface.&lt;/p&gt;

&lt;p&gt;It's how one computer (or software) can talk to another computer (or software).&lt;/p&gt;

&lt;p&gt;Basically, an API is where a computer or software application (the client) asks another computer or software application (the server) for some data or to do some task.&lt;/p&gt;

&lt;p&gt;In the case of humans, imagine I wanted to talk to you but you were in New York while I was in London. I can't just walk over to you and talk to you face-to-face. But, I can pick up my mobile phone and dial your number. That will cause your phone to ring. You pick up your phone, and we can talk through the use of our mobile phones. It's similar with computers; they can't talk to each other directly. They need an API to enable that communication.&lt;/p&gt;

&lt;p&gt;Another way to imagine it is by thinking about making an order at a restaurant. You, the customer, cannot talk directly to the chef and tell her what you want. Instead, you need to go through the waiter. The waiter takes your order and relays that to the chef. The chef then prepares what you want, gives it to the waiter, and the waiter gives it to you. In this example, you are the client computer, the chef is the server and the waiter is the API through which you and the chef communicate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An example of an API: SWAPI&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;S&lt;/strong&gt;tar &lt;strong&gt;W&lt;/strong&gt;ars &lt;strong&gt;API&lt;/strong&gt; (&lt;a href="https://swapi.dev/"&gt;https://swapi.dev/&lt;/a&gt;) is an API that provides data about Star Wars. If you ask it for planets/3/ it will give you all the data about planet 3.&lt;/p&gt;

&lt;h2&gt;
  
  
  OK, so now you sort of know what an API is. But what is a RESTful API?
&lt;/h2&gt;

&lt;p&gt;REST stands for &lt;strong&gt;RE&lt;/strong&gt;presentational &lt;strong&gt;S&lt;/strong&gt;tate &lt;strong&gt;T&lt;/strong&gt;ransfer.&lt;/p&gt;

&lt;p&gt;You might be asking yourself, "What in the world does that mean?" This &lt;a href="https://medium.com/the-sixt-india-blog/rest-stop-calling-your-http-apis-as-restful-apis-e8336e3e799b"&gt;article on Medium explains it really well&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It means when a RESTful API is called, the server will  &lt;em&gt;transfer&lt;/em&gt;  to the client a representation of the  &lt;em&gt;state&lt;/em&gt;  of the requested resource.&lt;/p&gt;

&lt;p&gt;For example, when a developer calls Instagram API to fetch a specific user (the resource), the API will return the state of that user, including their name, the number of posts that user posted on Instagram so far, how many followers they have, and more.&lt;/p&gt;

&lt;p&gt;The representation of the state can be in a JSON format, and probably for most APIs, this is indeed the case. It can also be in XML or HTML format.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Usually, a RESTful API will return a JSON data structure. JSON stands for &lt;strong&gt;J&lt;/strong&gt;ava &lt;strong&gt;S&lt;/strong&gt;cript &lt;strong&gt;O&lt;/strong&gt;bject &lt;strong&gt;N&lt;/strong&gt;otation and consists of key-value pairs.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP Requests and Responses
&lt;/h2&gt;

&lt;p&gt;HTTP is a communication protocol that computers (or applications) use to communicate with each other. It controls how computers communicate with each other.&lt;/p&gt;

&lt;p&gt;The client computer will send a HTTP request for what it wants to the server and the server will return a HTTP response. Note that a RESTful API usually uses HTTP, but it does not have to.&lt;/p&gt;

&lt;p&gt;There are four methods used by HTTP requests. These are known as the CRUD methods: &lt;strong&gt;C&lt;/strong&gt;reate, &lt;strong&gt;R&lt;/strong&gt;ead, &lt;strong&gt;U&lt;/strong&gt;pdate, and &lt;strong&gt;D&lt;/strong&gt;elete.&lt;/p&gt;

&lt;h3&gt;
  
  
  GET Requests
&lt;/h3&gt;

&lt;p&gt;Let's take an example: &lt;a href="http://www.facebook.com"&gt;www.facebook.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we want to go to Facebook, we are telling our browser to get us data from &lt;a href="http://www.facebook.com"&gt;www.facebook.com&lt;/a&gt; and then show it to us. We just want to load the website; we don't want to change or delete anything, we just want to see it. This is the most common type of request.&lt;/p&gt;

&lt;h3&gt;
  
  
  POST Requests
&lt;/h3&gt;

&lt;p&gt;A POST request is where we request to make a brand new resource. Ideally, when we make a POST request, it will return all the unique identifiers for that new resource to us so that we can then make a GET request using those unique identifiers.&lt;/p&gt;

&lt;p&gt;For example, say we want to create a new user on Facebook. We go to the sign up page and put in all our details to create the new user account. We then click 'submit', which sends the request to the server. The server receives it and recognises that it's a POST request (to make a new user; a new resource). It then makes the new user for us and sends us the login information for the new user. We can then use that login information to make a GET request to login to Facebook and see the contents of the page.&lt;/p&gt;

&lt;p&gt;Going back to the restaurant metaphor, this is like ordering a pizza: you tell the waiter that you want a Hawaiian pizza. The waiter then tells the chef that you want a Hawaiian pizza. The chef prepares the pizza for you, which the waiter then delivers to you. This is a POST request because you have asked the chef to prepare a brand new resource: a Hawaiian pizza.&lt;/p&gt;

&lt;h3&gt;
  
  
  DELETE Requests
&lt;/h3&gt;

&lt;p&gt;A DELETE request is where one computer asks another computer to delete a resource. For example, if we want to delete some photos from Facebook, or we want to delete a post from Instagram. When using DELETE, we need to be cautious that we don't delete entire lists by mistake! To avoid this, give the DELETE method a specific endpoint.&lt;/p&gt;

&lt;h3&gt;
  
  
  PATCH Requests
&lt;/h3&gt;

&lt;p&gt;A PATCH request is when we send a request to update a part of the resource.&lt;/p&gt;

&lt;p&gt;For example, taking our restaurant example, imagine that you've finished your meal and the waiter brings you the bill.&lt;/p&gt;

&lt;p&gt;While going through the bill to check it, you notice that you were charged the wrong amount for your Hawaiian pizza. You were charged $20 instead of $15. You did have pizza (and that is correctly recorded in the bill), but the amount charged is incorrect. You want that updated.&lt;/p&gt;

&lt;p&gt;You notify the waiter of the error and ask for the bill to be corrected. The waiter then tells the chef or whoever is in charge of the bill, the bill is then corrected and the new (corrected) bill is given to you.&lt;/p&gt;

&lt;p&gt;Another example would be when you ask a credit agency for your credit file and you want to check whether the details they have are correct. You make the request (GET request), the agency emails you the credit file and then you check it. You notice that they've got your title/prefix wrong: they've recorded you as Mr Sally Potter instead of Ms Sally Potter. You then send the agency an email asking them to correct this small error. This is a PATCH request - the file that the agency sent you was indeed the correct file (your credit file) and it contained all your details, but one of the pieces of information was wrong and you've asked for that to be corrected.&lt;/p&gt;

&lt;p&gt;Although we can use PATCH requests to update information, not all browsers and frameworks accept PATCH requests. This is where PUT requests come in (see below).&lt;/p&gt;

&lt;h3&gt;
  
  
  PUT Requests
&lt;/h3&gt;

&lt;p&gt;A PUT request is when you ask for an entire resource to be updated (not just a part of it).&lt;/p&gt;

&lt;p&gt;Going back to the restaurant example, this would be where you look at the bill and notice that you got charged for a beer which you didn't order or drink. What you actually ordered was a bottle of sparkling water. You therefore ask the waiter to please remove the charge for the beer from your bill and add in the sparkling water.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary Table for HTTP Requests
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;HTTP Method&lt;/th&gt;
&lt;th&gt;CRUD Operation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;td&gt;Create&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;READ&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PATCH&lt;/td&gt;
&lt;td&gt;Update or modify (partially)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PUT&lt;/td&gt;
&lt;td&gt;Update (fully) or replace&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DELETE&lt;/td&gt;
&lt;td&gt;Delete&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Responses
&lt;/h3&gt;

&lt;p&gt;When you make a request, the information is conveyed to the server and the server provides a &lt;strong&gt;response&lt;/strong&gt;. The response will be an HTTP status code.&lt;/p&gt;

&lt;p&gt;There are different HTTP response status codes, ranging from 100's to 500's. Usually, a response in the 200's indicates that things are good, and a response in the 400's means something on your end is not quite right. For a full list of the different HTTP response status codes visit the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status"&gt;MDN docs page on HTTP response status codes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Further reading&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you're curious and want to read more about Restful APIs and HTTP, here is a good &lt;a href="https://code.tutsplus.com/tutorials/a-beginners-guide-to-http-and-rest--net-16340"&gt;article about REST &amp;amp; HTTP&lt;/a&gt;. This &lt;a href="https://hevodata.com/learn/http-api-vs-rest-api/"&gt;article explains the difference between a REST API and a HTTP API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@marvelous?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Marvin Meyer&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/computer-network?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>codenewbie</category>
      <category>programmingbasics</category>
    </item>
    <item>
      <title>Basics of Z Shell for Newbie Developers</title>
      <dc:creator>Shalveena</dc:creator>
      <pubDate>Wed, 16 Mar 2022 20:43:38 +0000</pubDate>
      <link>https://dev.to/shalveena/basics-of-z-shell-for-newbie-developers-2fme</link>
      <guid>https://dev.to/shalveena/basics-of-z-shell-for-newbie-developers-2fme</guid>
      <description>&lt;p&gt;I’ve always been a bit scared of the terminal. It brings up images of a black screen with green text, with a hacker furiously typing away at the keyboard.&lt;/p&gt;

&lt;p&gt;The terminal is a powerful tool, and pretty unforgiving of mistakes. If you delete something by accident or mess something up, there’s usually no going back. &lt;/p&gt;

&lt;p&gt;While setting up my laptop for my new job, I was instructed to enter and execute various commands (that looked like jargon to me!) into the terminal. I had very little idea of what I was doing, but thankfully managed to bumble my way through without destroying anything too badly. But this prompted me to learn a bit more about the terminal and Z Shell, the shell used by most MacBooks.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the difference between the terminal and the shell?
&lt;/h2&gt;

&lt;p&gt;The shell is the program that processes the commands we input and outputs the results.&lt;/p&gt;

&lt;p&gt;The terminal is like a wrapper that goes around the shell. It allows us to enter commands and it translates the keystrokes for the shell. The terminal supports fonts, colours, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basics of using Z Shell
&lt;/h2&gt;

&lt;p&gt;Below are the basic commands used to navigate the file system using Z Shell.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;cd&lt;/code&gt; moves to your home directory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cd Documents&lt;/code&gt; moves to the Documents sub-folder&lt;/li&gt;
&lt;li&gt;If you want to move to a completely different folder (that is, not a sub-folder within your current directory), use the full pathname. For example: &lt;code&gt;cd /Applications/Rectangle.app&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Your home page is usually represented by the symbol ~ So if you want to access a folder that is inside your home directory, you can begin with ~ For example: &lt;code&gt;cd ~/Desktop&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cd ..&lt;/code&gt; takes you up one level (to the parent directory)
pwd prints the current working directory (the directory you’re currently in). This is very helpful if you don’t know where exactly you are.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ls&lt;/code&gt; lists the contents of the current directory. Note, you can also use this to find out the contents of a directory without actually going into that directory:
&lt;code&gt;ls ~/Desktop&lt;/code&gt; will show the contents of the Desktop folder.&lt;/li&gt;
&lt;li&gt;You can also add options to the ls command:
&lt;code&gt;// list all files (including hidden files)
ls -a 
// list files in long format
ls -l
// Note: you can also combine them. For example:
ls -la&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Viewing and editing files
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Viewing files
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;cat Documents/hello&lt;/code&gt; will display the text from the file named hello.&lt;/p&gt;

&lt;p&gt;If you try to use the cat command on a document that is not a text file, it may stuff up your terminal. If that happens, use the reset command to fix it: reset&lt;/p&gt;

&lt;p&gt;Alternatively, &lt;code&gt;less Documents/hello&lt;/code&gt; will also show you the content of the file, but with a bit more features. You navigate it using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;spacebar to go down a page&lt;/li&gt;
&lt;li&gt;b to move up&lt;/li&gt;
&lt;li&gt;q to exit&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Editing files
&lt;/h3&gt;

&lt;p&gt;For editing files, you can use Nano or Vim (both are usually already installed in most systems).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Nano:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;nano tiny_page&lt;/code&gt; will open the file named tiny page.&lt;/p&gt;

&lt;p&gt;The commands for Nano are then shown on screen. The only things to remember is that the ^ symbol refers to the control key, and “WriteOut” means to save!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Vim:&lt;/strong&gt;&lt;br&gt;
Vim is a pretty scary looking editor. Mostly because the commands and the way it works seems completely different to every other word editor and almost alien! But, it is also very powerful and has a lot of features. I am not a Vim power user, and have only learnt the very basics so that’s what I’m sharing here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;vim tiny_page&lt;/code&gt; will open up the file named tiny page.&lt;/li&gt;
&lt;li&gt;It first opens up in command mode, which means you cannot edit anything in the file. To edit, first enter insert mode by typing i You can then edit as you like.&lt;/li&gt;
&lt;li&gt;To exit insert mode, just press esc key.&lt;/li&gt;
&lt;li&gt;To save, press :w&lt;/li&gt;
&lt;li&gt;And, most importantly, to exit press :q or :q! to exit without saving.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Opening files
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;open hello&lt;/code&gt; will open the file named hello in using the default application.&lt;/li&gt;
&lt;li&gt;If you’ve configured VS Code to be your default editor for git, you can use &lt;code&gt;code hello&lt;/code&gt; to open the file named hello in VS Code.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;open .&lt;/code&gt; will open the current folder.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;open -a Preview picture.jpg&lt;/code&gt; will open picture.jpg in Preview.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;open -a Microsoft/ Word hello&lt;/code&gt; will open hello in Microsoft Word.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating files and folders
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;touch hello&lt;/code&gt; will create a file named hello. You can create multiple files in one command too: &lt;code&gt;touch file1 file2 file3&lt;/code&gt; will create three files; file1, file2, and file3.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mkdir&lt;/code&gt; will make a new folder. You can also create multiple folders in one command: &lt;code&gt;mkdir ~/folder1 ~/folder2&lt;/code&gt; will make two folders (folder 1 and folder 2) inside your home directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Moving and renaming files and folders
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mv source destination&lt;/code&gt; moves the file or folder from the source folder to the destination folder.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mv currentFileName newFileName&lt;/code&gt; can be used to rename files.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting help: man pages
&lt;/h2&gt;

&lt;p&gt;If you want more information about any of the commands, you can bring up the relevant man page by typing man followed by the command you want information about. For example: &lt;code&gt;man ls&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To navigate around the man page, use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;spacebar to move down a page&lt;/li&gt;
&lt;li&gt;b to move up&lt;/li&gt;
&lt;li&gt;/ to search&lt;/li&gt;
&lt;li&gt;n to go to the next match&lt;/li&gt;
&lt;li&gt;q to quit&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Other tips
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use the up and down arrow to cycle through previous commands you’ve used.&lt;/li&gt;
&lt;li&gt;Use tab to autocomplete file and folder names. This has the added advantage that there will be no typos in the names.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;file HelloWorld&lt;/code&gt; will show the type of the file. For example, whether it is a .jpg file or a .pdf file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These the basic commands that I’ve learnt so far. I’ll keep learning and sharing as I continue this journey as a budding software developer. Thanks for reading!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>wecoded</category>
      <category>womenintech</category>
    </item>
  </channel>
</rss>
