<?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: Alexej Bondarenko</title>
    <description>The latest articles on DEV Community by Alexej Bondarenko (@ersocon).</description>
    <link>https://dev.to/ersocon</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%2F1235562%2Fd9ea1309-a744-4b26-8451-aa358a2ca854.png</url>
      <title>DEV Community: Alexej Bondarenko</title>
      <link>https://dev.to/ersocon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ersocon"/>
    <language>en</language>
    <item>
      <title>Capitalizing the First Letter in JavaScript</title>
      <dc:creator>Alexej Bondarenko</dc:creator>
      <pubDate>Sat, 06 Jan 2024 08:29:49 +0000</pubDate>
      <link>https://dev.to/ersocon/capitalizing-the-first-letter-in-javascript-3g5k</link>
      <guid>https://dev.to/ersocon/capitalizing-the-first-letter-in-javascript-3g5k</guid>
      <description>&lt;p&gt;JavaScript is a versatile language, essential for creating dynamic and interactive web applications. One common task that developers often encounter is manipulating strings, particularly capitalizing the first letter. This seemingly simple task is crucial in enhancing the readability of text, adhering to grammatical rules, and improving user interfaces. In this article, we'll explore various methods to capitalize the first letter of a string in JavaScript, discussing their advantages and suitable scenarios.&lt;br&gt;
Understanding the Basics:&lt;/p&gt;

&lt;p&gt;Before delving into the solutions, let's understand what a string is in the context of JavaScript. A string is a sequence of characters used to represent text. In JavaScript, strings are immutable, meaning once created, they cannot be changed. However, you can create new strings based on operations performed on the original ones.&lt;/p&gt;
&lt;h2&gt;
  
  
  Method 1: Using charAt() and slice()
&lt;/h2&gt;

&lt;p&gt;One of the simplest ways to capitalize the first letter is by using the charAt() and slice() methods.&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="nf"&gt;capitalizeFirstLetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&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;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charAt&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;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;How it works:&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;charAt(0):&lt;/strong&gt; This method returns the first character of the string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;toUpperCase():&lt;/strong&gt; Converts the character to uppercase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;slice(1):&lt;/strong&gt; This method slices the string from the second character to the end.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Pros&lt;/u&gt;:&lt;br&gt;
Easy to understand and implement.&lt;br&gt;
Efficient for short strings.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Cons&lt;/u&gt;:&lt;br&gt;
Not the most concise method.&lt;/p&gt;
&lt;h2&gt;
  
  
  Method 2: Using Regular Expressions
&lt;/h2&gt;

&lt;p&gt;For those comfortable with regular expressions, this method offers a concise way to capitalize the first letter.&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="nf"&gt;capitalizeFirstLetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&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;string&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="sr"&gt;/^./&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;string&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;toUpperCase&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;&lt;u&gt;How it works:&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;replace()&lt;/strong&gt;: This method replaces the first character (matched by the regular expression ^.) with its uppercase version.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Pros&lt;/u&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Concise and elegant.&lt;/li&gt;
&lt;li&gt;Powerful in handling more complex patterns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;Cons&lt;/u&gt;:&lt;/p&gt;

&lt;p&gt;-Less readable for those not familiar with regular expressions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 3: Using ES6 Features
&lt;/h2&gt;

&lt;p&gt;If you're working in an environment that supports ES6 features, template literals and destructuring can offer a clean solution.&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;const&lt;/span&gt; &lt;span class="nx"&gt;capitalizeFirstLetter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;How it works&lt;/u&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Destructuring assignment is used to split the first character and the rest of the string.&lt;/li&gt;
&lt;li&gt;Template literals then reconstruct the string with the capitalized first letter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;Pros&lt;/u&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clean and modern syntax.&lt;/li&gt;
&lt;li&gt;Expressive and concise.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;Cons&lt;/u&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only suitable for environments that support ES6.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices and Considerations:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Check for Empty Strings&lt;/strong&gt;: Always check if the string is empty before attempting to capitalize it to avoid errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Locale Considerations&lt;/strong&gt;: For applications supporting multiple languages, consider using toLocaleUpperCase() instead of toUpperCase() to respect locale-specific rules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: For applications dealing with large volumes of text, test the performance of your chosen method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Capitalizing the first letter of a string in JavaScript is a common task that can be achieved through various methods. Whether you prefer the straightforward approach of combining charAt() and slice(), the elegance of regular expressions, or the modern syntax of ES6 features, there's a solution that fits your needs and coding style. &lt;/p&gt;

&lt;p&gt;Understanding these methods not only helps in text manipulation tasks but also enhances your overall JavaScript proficiency. So, next time you're faced with a string manipulation challenge, you'll be well-equipped to handle it with grace and efficiency.&lt;/p&gt;

&lt;p&gt;If you are interested in how other languages handle the same question, I have blogged about the same topic for PureScript in my article "&lt;a href="https://www.ersocon.net/articles/capitalizing-the-first-letter-in-purescript~5c85c942-52fd-4f1c-a288-fcbf89d739e0?utm_source=devto"&gt;Capitalizing the First Letter in PureScript&lt;/a&gt;".&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Lists and LinkedLists in JavaScript</title>
      <dc:creator>Alexej Bondarenko</dc:creator>
      <pubDate>Sat, 06 Jan 2024 08:12:51 +0000</pubDate>
      <link>https://dev.to/ersocon/lists-and-linkedlists-in-javascript-2j0h</link>
      <guid>https://dev.to/ersocon/lists-and-linkedlists-in-javascript-2j0h</guid>
      <description>&lt;p&gt;Recently I was asked in an interview the question about Lists and LinkedLists in Java. After the interview I came up as well with the idea to see how other languages handle this.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;JavaScript&lt;/strong&gt;, the concept of lists and linked lists is quite different from their implementation in &lt;strong&gt;Java&lt;/strong&gt;, largely due to the nature of the languages themselves. The following are the differences and the underlying reasons:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Introduction
&lt;/h2&gt;

&lt;p&gt;In the realm of programming, data structures are fundamental. Among these, lists and linked lists are crucial for storing and manipulating collections of data. While languages like Java have explicit classes for these structures, JavaScript's approach is more implicit, owing to its dynamic and flexible nature.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Lists in Java vs. JavaScript
&lt;/h2&gt;

&lt;p&gt;In Java, a list is an interface, and it is part of Java's Collections Framework. It defines an ordered collection, and classes like ArrayList and LinkedList provide concrete implementations. These lists come with various methods for data manipulation, ensuring type safety and offering performance benefits for different use cases.&lt;/p&gt;

&lt;p&gt;In contrast, JavaScript does not have a built-in list data structure. The closest equivalent is an array, which is a high-level, list-like object. JavaScript arrays are dynamic and can hold a mix of different data types. They provide methods similar to Java's lists, but without the type safety and with some differences in performance characteristics.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. LinkedList in Java
&lt;/h2&gt;

&lt;p&gt;In Java, LinkedList is a doubly-linked list implementation of the List and Deque interfaces. It allows for constant-time insertions and deletions at both ends and is efficient for manipulating data. However, it has slower random access compared to an ArrayList.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Linked Lists in JavaScript
&lt;/h2&gt;

&lt;p&gt;JavaScript does not have a built-in linked list class. However, its dynamic nature allows for easy implementation of a linked list from scratch. A JavaScript linked list typically involves creating a Node class and a LinkedList class with methods for addition, deletion, and traversal.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Why the Difference?
&lt;/h2&gt;

&lt;p&gt;The key reason for this difference lies in the language design philosophies:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Java:&lt;/strong&gt; Java is statically typed and has a strong emphasis on explicitness and object-oriented design. The Collections Framework in Java provides a standardized way of handling data structures, ensuring type safety and predictability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JavaScript:&lt;/strong&gt; JavaScript is dynamically typed and prioritizes flexibility and ease of use. It does not enforce a strict data structure paradigm, allowing developers to implement structures like linked lists as needed, using the basic objects and arrays.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Practical Implications
&lt;/h2&gt;

&lt;p&gt;For JavaScript developers, this means that understanding the underlying principles of data structures is essential. While JavaScript does not provide these structures out of the box, its flexibility allows for tailored implementations that can be optimized for specific use cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Summary
&lt;/h2&gt;

&lt;p&gt;In conclusion, the absence of explicit list and linked list structures in JavaScript, as opposed to Java, is a reflection of the language's design and philosophy. JavaScript's dynamic arrays and the ability to implement custom data structures offer a different, more flexible approach to data management. Understanding these differences is crucial for developers transitioning between languages or working in multi-language environments.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Fetch the Bitcoin Price from Kraken API in PureScript</title>
      <dc:creator>Alexej Bondarenko</dc:creator>
      <pubDate>Tue, 19 Dec 2023 09:39:10 +0000</pubDate>
      <link>https://dev.to/ersocon/how-to-fetch-the-bitcoin-price-from-kraken-api-in-purescript-i8o</link>
      <guid>https://dev.to/ersocon/how-to-fetch-the-bitcoin-price-from-kraken-api-in-purescript-i8o</guid>
      <description>&lt;p&gt;In the fast-evolving world of cryptocurrencies, staying updated with real-time prices is crucial for both enthusiasts and investors. Bitcoin, being the pioneer and most valued cryptocurrency, holds significant interest. This article delves into how one can harness the capabilities of PureScript, a powerful and expressive functional programming language, to fetch Bitcoin prices from the Kraken API.&lt;/p&gt;

&lt;p&gt;PureScript, often likened to Haskell, is a strongly-typed language that compiles to JavaScript. It's known for its robust type system and functional programming paradigms, making it an excellent choice for writing reliable, maintainable web applications. Its strong type system can enforce correctness at compile time, reducing runtime errors and enhancing code quality.&lt;/p&gt;

&lt;p&gt;Kraken, on the other hand, is one of the largest and most respected cryptocurrency exchanges in the world. It offers a comprehensive API that provides access to real-time and historical market data, including Bitcoin prices. Utilizing this API, developers can integrate cryptocurrency data into their applications or perform analysis.&lt;/p&gt;

&lt;p&gt;In this article, we will guide you through setting up a PureScript environment, understanding the basics of the Kraken API, and step by step, demonstrate how to write a PureScript application to fetch the current Bitcoin price. This journey will not only enhance your understanding of PureScript and APIs but also provide you with a practical tool for cryptocurrency data retrieval.&lt;/p&gt;

&lt;p&gt;Whether you are a seasoned PureScript developer or new to the language, this guide aims to provide valuable insights and practical steps to effectively interact with the Kraken API. So, let's dive in and explore the intersection of functional programming and cryptocurrency data fetching.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up Your PureScript Environment
&lt;/h2&gt;

&lt;p&gt;Getting started with PureScript requires setting up the appropriate development environment. This section will guide you through the installation of the PureScript compiler, Spago (the PureScript package manager and build tool), and the creation of a new PureScript project.&lt;br&gt;
Installing PureScript&lt;br&gt;
Prerequisites&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node.js&lt;/strong&gt;: PureScript compiles to JavaScript, so Node.js is required to run the resulting code. Install the latest version of Node.js from nodejs.org.&lt;/p&gt;
&lt;h3&gt;
  
  
  Installation Steps
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Install PureScript&lt;/strong&gt;: Use npm (Node.js package manager) to install PureScript globally. Open your terminal or command prompt and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; purescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Verify Installation&lt;/strong&gt;: Ensure PureScript is installed correctly by checking its version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;purs &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Installing Spago
&lt;/h3&gt;

&lt;p&gt;Spago is a PureScript package manager and build tool, designed to work with the PureScript package set.&lt;br&gt;
Installation Steps&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install Spago&lt;/strong&gt;: Again, use npm to install Spago globally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; spago
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Verify Installation&lt;/strong&gt;: Check if Spago is installed properly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;spago &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Setting Up a New PureScript Project
&lt;/h3&gt;

&lt;p&gt;Now that you have PureScript and Spago installed, you can set up a new PureScript project.&lt;br&gt;
Creating the Project&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a New Directory&lt;/strong&gt;: Make a new directory for your project and navigate into it:&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;mkdir &lt;/span&gt;my-purescript-project
&lt;span class="nb"&gt;cd &lt;/span&gt;my-purescript-project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Initialize the Project&lt;/strong&gt;: Use Spago to initialize a new PureScript project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;spago init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will create a new PureScript project in your current directory. It includes some basic files:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;src/Main.purs&lt;/strong&gt;: A sample PureScript source file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;test/Main.purs&lt;/strong&gt;: A sample test file for your project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;spago.dhall&lt;/strong&gt;: Configuration file for your project dependencies and build settings.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exploring the Project
&lt;/h3&gt;

&lt;p&gt;Take a moment to explore the structure of your new PureScript project. The src directory will hold your PureScript code, and test is for your test files. The spago.dhall file allows you to manage dependencies and specify project settings.&lt;br&gt;
Next Steps&lt;/p&gt;

&lt;p&gt;With your PureScript environment set up, you're ready to start writing PureScript code and explore its capabilities. The next section will introduce you to the Kraken API, which we will use to fetch Bitcoin prices.&lt;/p&gt;
&lt;h2&gt;
  
  
  Understanding the Kraken API
&lt;/h2&gt;

&lt;p&gt;Before diving into coding, it's crucial to understand the Kraken API, which will be our gateway to fetching Bitcoin prices. This section will provide an overview of what the Kraken API offers, focusing on the specific endpoint we'll use for retrieving Bitcoin prices.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is the Kraken API?
&lt;/h3&gt;

&lt;p&gt;Kraken is a popular cryptocurrency exchange that provides a feature-rich API. This API allows developers to access market data, manage accounts, execute trades, and more. For our purpose, we are interested in the public market data that Kraken provides, specifically the current price of Bitcoin.&lt;br&gt;
Key Features of the Kraken API&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Public and Private Endpoints&lt;/strong&gt;: The API offers both public and private endpoints. Public endpoints provide market data like asset prices, order book details, and recent trades.&lt;/p&gt;

&lt;p&gt;Private endpoints, which require authentication, are for managing accounts and executing trades.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rate Limits&lt;/strong&gt;: Kraken imposes rate limits to ensure fair usage of their API. It’s important to be aware of these limits when designing your application to avoid being temporarily banned from the API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Format&lt;/strong&gt;: Responses from the Kraken API are typically in JSON format, making it easy to parse and use in your application.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Endpoint for Bitcoin Prices
&lt;/h3&gt;

&lt;p&gt;To fetch Bitcoin prices, we will use a specific public endpoint provided by Kraken:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Endpoint URL&lt;/strong&gt;: &lt;a href="https://api.kraken.com/0/public/Ticker"&gt;https://api.kraken.com/0/public/Ticker&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Query Parameters&lt;/strong&gt;: To retrieve Bitcoin data, we need to specify the pair parameter. For Bitcoin-to-USD, the parameter is pair=XBTUSD.&lt;/p&gt;
&lt;h3&gt;
  
  
  Understanding the Response
&lt;/h3&gt;

&lt;p&gt;The response from this endpoint will be in JSON format and will contain several pieces of information, including the current ask price, bid price, and last traded price. Here’s a simplified example of what the response might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"error"&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="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"result"&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="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"XXBTZUSD"&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="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"a"&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="s2"&gt;"50000.00000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.000"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&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="s2"&gt;"49995.00000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.000"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"c"&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="s2"&gt;"50000.00000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"0.00345678"&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="w"&gt;
  &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="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this response:&lt;/p&gt;

&lt;p&gt;a is the ask array, where a[0] is the best ask price.&lt;br&gt;
b is the bid array, with b[0] being the best bid price.&lt;br&gt;
c is the last trade closed array, c[0] being the last trade price.&lt;/p&gt;
&lt;h3&gt;
  
  
  Next Steps
&lt;/h3&gt;

&lt;p&gt;With a clear understanding of the Kraken API, particularly the endpoint for fetching Bitcoin prices, we are now ready to move into the practical aspects of using PureScript to make HTTP requests and process this data.&lt;/p&gt;

&lt;p&gt;In the following sections, we will discuss how to make HTTP requests in PureScript, parse the JSON responses, and specifically extract the Bitcoin price information from the Kraken API's response. This will involve exploring some of the functional programming paradigms that PureScript offers, ensuring a robust and type-safe approach to interacting with external APIs.&lt;/p&gt;
&lt;h2&gt;
  
  
  Making HTTP Requests in PureScript
&lt;/h2&gt;

&lt;p&gt;To fetch the Bitcoin price from the Kraken API, we need to make HTTP requests from our PureScript application. PureScript offers several libraries for handling HTTP requests and responses, one of which is purescript-affjax. This section will guide you through using this library to send requests to the Kraken API and handle the responses.&lt;/p&gt;
&lt;h3&gt;
  
  
  Understanding purescript-affjax
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;purescript-affjax&lt;/strong&gt; is a popular library in the PureScript ecosystem for making asynchronous HTTP requests. It uses the Aff monad, which handles asynchronous code in a functional way. This library simplifies the process of making HTTP requests and parsing responses.&lt;br&gt;
Installing purescript-affjax&lt;/p&gt;

&lt;p&gt;Before you can use purescript-affjax, you need to add it to your project. You can do this using Spago, the PureScript package manager.&lt;/p&gt;

&lt;p&gt;Add the Dependency: Run the following command in your project directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;spago &lt;span class="nb"&gt;install &lt;/span&gt;affjax
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Making a GET Request
&lt;/h3&gt;

&lt;p&gt;To fetch data from the Kraken API, we will use a GET request. Here’s a basic example of how to make a GET request to the Kraken API using purescript-affjax.&lt;br&gt;
Importing Necessary Modules&lt;/p&gt;

&lt;p&gt;First, import the necessary modules in your PureScript file:&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;Prelude&lt;/span&gt;

&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Affjax&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;AJAX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Affjax.ResponseFormat&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Effect.Aff&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;launchAff_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Effect.Class.Console&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Writing the Request Function
&lt;/h3&gt;

&lt;p&gt;Now, let's write a function to perform the GET request:&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;fetchBitcoinPrice&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Effect&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;
&lt;span class="n"&gt;fetchBitcoinPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;launchAff_&lt;/span&gt; &lt;span class="kr"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="s"&gt;"https://api.kraken.com/0/public/Ticker?pair=XBTUSD"&lt;/span&gt;
  &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
    &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="s"&gt;"Request failed: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;show&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
    &lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="s"&gt;"Received response: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;show&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this function:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;launchAff_&lt;/strong&gt; is used to execute an asynchronous action.&lt;br&gt;
    get json makes a GET request and expects a JSON response.&lt;/p&gt;

&lt;p&gt;The URL "&lt;a href="https://api.kraken.com/0/public/Ticker?pair=XBTUSD"&gt;https://api.kraken.com/0/public/Ticker?pair=XBTUSD&lt;/a&gt;" is the Kraken API endpoint for the Bitcoin price.&lt;/p&gt;

&lt;p&gt;The response is either an error (Left error) or a successful response (Right res), which we log to the console. (If you are interested in more details about this topic, I have written about it in detail in the following article: &lt;a href="https://www.ersocon.net/articles/get-request-in-purescript~1642dcba-54d9-4d61-aa39-a0c2b37d111e"&gt;PureScript Get Request - How to Make a HTTP Request in PS&lt;/a&gt; )&lt;/p&gt;
&lt;h3&gt;
  
  
  Running the Function
&lt;/h3&gt;

&lt;p&gt;To run this function, you can call fetchBitcoinPrice from your Main.purs file or wherever you handle your application's entry point.&lt;/p&gt;
&lt;h3&gt;
  
  
  Next Steps
&lt;/h3&gt;

&lt;p&gt;Having set up the basic structure for making HTTP requests, the next step is to parse the JSON response from the Kraken API. We will need to define PureScript types that correspond to the structure of the API response and write functions to extract the Bitcoin price information. This will involve using PureScript's strong type system to ensure that our data handling is both correct and efficient.&lt;/p&gt;
&lt;h2&gt;
  
  
  Parsing API Responses
&lt;/h2&gt;

&lt;p&gt;After making an HTTP request to the Kraken API, the next crucial step is parsing the JSON response to extract meaningful data. PureScript, with its strong type system, offers a reliable way to handle JSON parsing. This section will guide you through the process of parsing the Kraken API response and extracting the Bitcoin price information.&lt;br&gt;
Understanding JSON Parsing in PureScript&lt;/p&gt;

&lt;p&gt;In PureScript, JSON parsing is typically done using the &lt;strong&gt;purescript-foreign-generic&lt;/strong&gt; library, which provides tools for decoding and encoding JSON in a type-safe manner. This involves defining PureScript types that mirror the structure of the JSON data you expect to receive and then using these types to decode the JSON.&lt;/p&gt;
&lt;h3&gt;
  
  
  Installing purescript-foreign-generic
&lt;/h3&gt;

&lt;p&gt;If you haven’t already, you'll need to add purescript-foreign-generic to your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;spago &lt;span class="nb"&gt;install &lt;/span&gt;foreign-generic
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Defining PureScript Types for Kraken Response
&lt;/h3&gt;

&lt;p&gt;Based on the structure of the Kraken API response, we'll define corresponding PureScript types. Here's an 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;import&lt;/span&gt; &lt;span class="nn"&gt;Prelude&lt;/span&gt;

&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Data.Foreign.Class&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Decode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Encode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Data.Foreign.Generic&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defaultOptions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;genericDecode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;genericEncode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Foreign.Class&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ReadForeign&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;-- Define a type for the response&lt;/span&gt;
&lt;span class="kr"&gt;newtype&lt;/span&gt; &lt;span class="kt"&gt;KrakenResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;KrakenResponse&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Result&lt;/span&gt;
  &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Array&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;derive&lt;/span&gt; &lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="n"&gt;genericKrakenResponse&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Generic&lt;/span&gt; &lt;span class="kt"&gt;KrakenResponse&lt;/span&gt; &lt;span class="kr"&gt;_&lt;/span&gt;
&lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="n"&gt;decodeKrakenResponse&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Decode&lt;/span&gt; &lt;span class="kt"&gt;KrakenResponse&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
  &lt;span class="n"&gt;decode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;genericDecode&lt;/span&gt; &lt;span class="n"&gt;defaultOptions&lt;/span&gt;
&lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="n"&gt;encodeKrakenResponse&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Encode&lt;/span&gt; &lt;span class="kt"&gt;KrakenResponse&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
  &lt;span class="n"&gt;encode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;genericEncode&lt;/span&gt; &lt;span class="n"&gt;defaultOptions&lt;/span&gt;

&lt;span class="c1"&gt;-- Define a type for the Result&lt;/span&gt;
&lt;span class="kr"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Record&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Ticker&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Ticker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;-- Define a type for the Ticker&lt;/span&gt;
&lt;span class="kr"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Ticker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Record&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;lastTradePrice&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Array&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;derive&lt;/span&gt; &lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="n"&gt;genericTicker&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Generic&lt;/span&gt; &lt;span class="kt"&gt;Ticker&lt;/span&gt; &lt;span class="kr"&gt;_&lt;/span&gt;
&lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="n"&gt;decodeTicker&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Decode&lt;/span&gt; &lt;span class="kt"&gt;Ticker&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
  &lt;span class="n"&gt;decode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;genericDecode&lt;/span&gt; &lt;span class="n"&gt;defaultOptions&lt;/span&gt;
&lt;span class="kr"&gt;instance&lt;/span&gt; &lt;span class="n"&gt;encodeTicker&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Encode&lt;/span&gt; &lt;span class="kt"&gt;Ticker&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;
  &lt;span class="n"&gt;encode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;genericEncode&lt;/span&gt; &lt;span class="n"&gt;defaultOptions&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Parsing the JSON Response
&lt;/h3&gt;

&lt;p&gt;With the types defined, we can now parse the JSON response from the Kraken API:&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;parseKrakenResponse&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Either&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="kt"&gt;KrakenResponse&lt;/span&gt;
&lt;span class="n"&gt;parseKrakenResponse&lt;/span&gt; &lt;span class="n"&gt;jsonString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="n"&gt;read&lt;/span&gt; &lt;span class="n"&gt;jsonString&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
    &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="s"&gt;"Error parsing JSON: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;show&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
    &lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="n"&gt;value&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;decode&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
      &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="s"&gt;"Error decoding KrakenResponse: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;show&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
      &lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this function, read attempts to parse the raw JSON string, and decode tries to convert the parsed JSON into our KrakenResponse type. The function returns either an error message or the successfully parsed response.&lt;br&gt;
Extracting Bitcoin Price&lt;/p&gt;

&lt;p&gt;Once we have the KrakenResponse, we can extract the Bitcoin price:&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;extractBitcoinPrice&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;KrakenResponse&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Maybe&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="n"&gt;extractBitcoinPrice&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;XXBTZUSD&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lastTradePrice&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function navigates through the &lt;strong&gt;KrakenResponse&lt;/strong&gt; to find the last traded price of Bitcoin.&lt;/p&gt;

&lt;h3&gt;
  
  
  Next Steps
&lt;/h3&gt;

&lt;p&gt;Now that we have the mechanism to parse the Kraken API response and extract the Bitcoin price, the final step is to integrate this parsing logic with our HTTP request function. We will update the function to not only fetch data from the Kraken API but also to parse the response and extract the relevant Bitcoin price information, handling any potential errors along the way. This will complete our PureScript application for fetching Bitcoin prices from the Kraken API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fetching Bitcoin Price from Kraken
&lt;/h2&gt;

&lt;p&gt;Having set up the HTTP request functionality and the JSON parsing mechanism, we are now ready to combine these components to fetch the current Bitcoin price from the Kraken API. This section will walk you through the process of integrating the HTTP request with the JSON parsing to retrieve and display the Bitcoin price.&lt;/p&gt;

&lt;h3&gt;
  
  
  Integrating HTTP Requests with JSON Parsing
&lt;/h3&gt;

&lt;p&gt;We'll update our fetchBitcoinPrice function to not only make the HTTP request but also to parse the response and extract the Bitcoin price.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Updated fetchBitcoinPrice Function&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;import&lt;/span&gt; &lt;span class="nn"&gt;Prelude&lt;/span&gt;

&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Affjax&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;AJAX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Affjax.ResponseFormat&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Effect.Aff&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Aff&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;launchAff_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Effect.Class.Console&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;-- Other imports and type definitions from previous sections go here&lt;/span&gt;

&lt;span class="n"&gt;fetchBitcoinPrice&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Aff&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;
&lt;span class="n"&gt;fetchBitcoinPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="s"&gt;"https://api.kraken.com/0/public/Ticker?pair=XBTUSD"&lt;/span&gt;
  &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
    &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
      &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="s"&gt;"Request failed: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;show&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
    &lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="n"&gt;res&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;parseKrakenResponse&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;show&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
        &lt;span class="kt"&gt;Left&lt;/span&gt; &lt;span class="n"&gt;parseError&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
          &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="s"&gt;"Parsing failed: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;parseError&lt;/span&gt;
        &lt;span class="kt"&gt;Right&lt;/span&gt; &lt;span class="n"&gt;krakenResponse&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;extractBitcoinPrice&lt;/span&gt; &lt;span class="n"&gt;krakenResponse&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
            &lt;span class="kt"&gt;Nothing&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
              &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="s"&gt;"Could not extract Bitcoin price."&lt;/span&gt;
            &lt;span class="kt"&gt;Just&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
              &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="s"&gt;"The current Bitcoin price is: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;

&lt;span class="n"&gt;launchFetch&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Effect&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;
&lt;span class="n"&gt;launchFetch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;launchAff_&lt;/span&gt; &lt;span class="n"&gt;fetchBitcoinPrice&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  In this updated function:
&lt;/h2&gt;

&lt;p&gt;We make the GET request to the Kraken API.&lt;/p&gt;

&lt;p&gt;If the request is successful, we attempt to parse the response using parseKrakenResponse.&lt;/p&gt;

&lt;p&gt;If parsing is successful, we extract the Bitcoin price using extractBitcoinPrice.&lt;/p&gt;

&lt;p&gt;Finally, we log the result to the console.&lt;/p&gt;

&lt;h3&gt;
  
  
  Error Handling
&lt;/h3&gt;

&lt;p&gt;The function includes error handling at each step:&lt;/p&gt;

&lt;p&gt;If the HTTP request fails, we log the request error.&lt;br&gt;
If parsing the response fails, we log the parsing error.&lt;br&gt;
If we cannot extract the Bitcoin price, we log an appropriate message.&lt;/p&gt;
&lt;h3&gt;
  
  
  Running the Application
&lt;/h3&gt;

&lt;p&gt;To run the application and fetch the Bitcoin price, call launchFetch from your application's entry point, typically in the Main.purs file.&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;module&lt;/span&gt; &lt;span class="nn"&gt;Main&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;

&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Prelude&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Effect&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Effect&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;MyModule&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;launchFetch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;-- Replace with the actual module name&lt;/span&gt;

&lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Effect&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;
&lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;launchFetch&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With the fetchBitcoinPrice function in place, your PureScript application is now capable of fetching real-time Bitcoin price data from the Kraken API. This not only demonstrates the power of PureScript in handling HTTP requests and JSON parsing but also gives you a practical tool for cryptocurrency data retrieval.&lt;/p&gt;

&lt;p&gt;Remember, this example can be extended or modified to fetch different types of data from the Kraken API or other similar services, showcasing the versatility and capability of &lt;strong&gt;PureScript&lt;/strong&gt; in working with external APIs.&lt;/p&gt;

</description>
      <category>purescript</category>
      <category>bitcoin</category>
      <category>programming</category>
      <category>api</category>
    </item>
    <item>
      <title>Top 5 Interview Questions for Haskell Developers</title>
      <dc:creator>Alexej Bondarenko</dc:creator>
      <pubDate>Mon, 18 Dec 2023 21:33:42 +0000</pubDate>
      <link>https://dev.to/ersocon/top-5-interview-questions-for-haskell-developers-8po</link>
      <guid>https://dev.to/ersocon/top-5-interview-questions-for-haskell-developers-8po</guid>
      <description>&lt;p&gt;Haskell, known for its strong static typing, purity, and elegant syntax, is a favorite among functional programming aficionados. When interviewing Haskell developers, it's crucial to assess not just their coding skills but also their understanding of functional programming principles and Haskell-specific features. Here are the top 5 interview questions that can help evaluate a candidate's proficiency in Haskell.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Explain Monads and Their Significance in Haskell
&lt;/h2&gt;

&lt;p&gt;Monads are a fundamental concept in Haskell, crucial for managing side effects and building complex functional pipelines. A good answer would include:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt; Monads are a type of functor, a structure that represents computations instead of values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Significance:&lt;/strong&gt; They allow for the chaining of operations and managing of side effects in a functional manner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples:&lt;/strong&gt; Maybe, IO, and List are common monads in Haskell, each serving different purposes like handling nullability, input/output operations, and list processing.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Discuss Lazy Evaluation and Its Benefits and Drawbacks
&lt;/h2&gt;

&lt;p&gt;Lazy evaluation is a key feature of Haskell. A well-rounded answer should address:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Concept:&lt;/strong&gt; Lazy evaluation means that expressions are not evaluated until their values are needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits:&lt;/strong&gt; It allows for the creation of more efficient, modular code, and the ability to work with infinite data structures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Drawbacks:&lt;/strong&gt; Potential issues with space leaks and difficulties in predicting performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. How Does Haskell Handle Null Values? Explain with an Example
&lt;/h2&gt;

&lt;p&gt;Haskell's approach to null values is distinct. Candidates should mention:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Maybe Type&lt;/strong&gt;: Haskell uses the Maybe type to handle null values, which can either be Just a (representing a value) or Nothing (representing the absence of a value).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: A function to safely extract the head of a list might return Maybe a, with Just a if the list is not empty, and Nothing if it is.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Describe Type Classes in Haskell and Their Purpose
&lt;/h2&gt;

&lt;p&gt;Type classes are central to Haskell's type system. An insightful answer would include:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt; Type classes define a set of functions that can operate on multiple types.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; They allow for polymorphism, enabling functions to operate on different types while ensuring type safety&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Discussing the Eq type class, which includes types that can be compared for equality.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Can You Demonstrate a Real-World Problem Solved More Efficiently with Haskell?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This open-ended question assesses practical Haskell application. Look for:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem Description:&lt;/strong&gt; An explanation of a problem that fits well with functional programming paradigms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Haskell Solution:&lt;/strong&gt; How Haskell's features, like higher-order functions, purity, or strong typing, provide an efficient solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Comparison:&lt;/strong&gt; A comparison with how the same problem might be solved in a more imperative language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;These questions delve into core Haskell concepts and practices, providing a comprehensive framework to gauge a developer's expertise in Haskell. Remember, the best candidates not only provide accurate answers but also demonstrate a deep understanding of functional programming principles and how they apply in real-world scenarios.&lt;/p&gt;

</description>
      <category>haskell</category>
      <category>interview</category>
      <category>programming</category>
      <category>career</category>
    </item>
    <item>
      <title>Understanding import and require in JavaScript: A Guide for Beginners</title>
      <dc:creator>Alexej Bondarenko</dc:creator>
      <pubDate>Mon, 18 Dec 2023 20:48:41 +0000</pubDate>
      <link>https://dev.to/ersocon/understanding-import-and-require-in-javascript-a-guide-for-beginners-j7h</link>
      <guid>https://dev.to/ersocon/understanding-import-and-require-in-javascript-a-guide-for-beginners-j7h</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZjL6KTjT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcfotgdeukq3j3xnoyaf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZjL6KTjT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcfotgdeukq3j3xnoyaf.png" alt="JavaScript require and import" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JavaScript, a versatile and widely-used programming language, offers two primary methods for importing modules or libraries into your code: import and require. This article aims to clarify these methods for beginner developers, detailing their differences and providing examples of their usage.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. require: CommonJS Module Syntax
&lt;/h2&gt;

&lt;p&gt;The require function is part of the CommonJS module syntax, predominantly used in Node.js environments. It's known for its dynamic nature, allowing developers to conditionally and programmatically load modules.&lt;br&gt;
How require Works&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;: const moduleName = require('module-name');&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functionality&lt;/strong&gt;: Reads a JavaScript file, executes it, and then proceeds to return the exports object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case&lt;/strong&gt;: Typically used in server-side Node.js applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&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="c1"&gt;// Importing the 'fs' (file system) module using require&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Using the imported module to read a file&lt;/span&gt;
&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;example.txt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&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;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;h2&gt;
  
  
  2. import: ES6 Module Syntax
&lt;/h2&gt;

&lt;p&gt;The import statement, introduced in ECMAScript 2015 (ES6), is used to import functions, objects, or primitives from external modules or files.&lt;br&gt;
How import Works&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;: import { moduleName } from 'module-name';&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functionality&lt;/strong&gt;: It's more static and operates at compile-time, meaning it cannot be called conditionally within the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case&lt;/strong&gt;: Common in front-end JavaScript frameworks like React, Angular, and Vue.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&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="c1"&gt;// Importing a specific function from a module&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;readFile&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Using the imported function&lt;/span&gt;
&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;example.txt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&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;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;h2&gt;
  
  
  Key Differences
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Environment&lt;/strong&gt;: require is specific to Node.js, while import can be used in both browser and Node.js environments (with transpilers or Node.js versions that support ES Modules).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;: require uses assignment syntax, whereas import has a declarative syntax.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Loading Time&lt;/strong&gt;: require is runtime/dynamic, and import is static and happens at compile-time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conditional Loading&lt;/strong&gt;: With require, you can conditionally load modules, but import doesn't support this directly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Native Support&lt;/strong&gt;: Most modern browsers support import natively, but require needs a bundler like Webpack for browser use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding the difference between import and require is crucial for JavaScript developers, especially when working across different environments like the server (Node.js) and the browser. While require offers flexibility with dynamic loading, import provides a more declarative and static approach, often resulting in better optimization and error handling.&lt;/p&gt;

&lt;p&gt;As you continue your JavaScript journey, experiment with both methods to gain a deeper understanding of their applications and advantages. Remember, the choice between import and require largely depends on your project's environment and specific requirements.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Bunny CDN Purge Cache in Haskell IHP</title>
      <dc:creator>Alexej Bondarenko</dc:creator>
      <pubDate>Sun, 17 Dec 2023 23:38:45 +0000</pubDate>
      <link>https://dev.to/ersocon/bunny-cdn-purge-cache-in-haskell-ihp-13a2</link>
      <guid>https://dev.to/ersocon/bunny-cdn-purge-cache-in-haskell-ihp-13a2</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fKUDaFji--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tkyrcwltocb4m2hzwpw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fKUDaFji--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8tkyrcwltocb4m2hzwpw.png" alt="Image description" width="800" height="456"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://bunny.net/?ref=n86usfpw93"&gt;Bunny CDN&lt;/a&gt; is a content delivery network (CDN) service. It's designed to help improve the loading times and performance of websites by distributing content across multiple servers located around the world. Our own site - the one that you are reading right now - is using this kind of CDN as well.While writing or correcting articles we came up with the requirement to purge cached articles as soon as they update.&lt;/p&gt;

&lt;p&gt;Bunny.net provides an API that allows developers to integrate its services with their applications or websites. This API can be used for various tasks like purging cached content, managing zones, or retrieving usage statistics.&lt;br&gt;
The General Approach to Purge Cache&lt;/p&gt;

&lt;p&gt;To create a Haskell program that purges a given URL using the bunny.net API, you would typically use an HTTP client library for Haskell, like http-conduit or wreq. For this article, I'll use http-conduit as it's quite popular and versatile. With the help of this library we will asynchronously send a request to the Bunny API Endpoint.&lt;br&gt;
Implementing purgeCache&lt;/p&gt;

&lt;p&gt;First, you need to install the http-conduit package if you haven't already. You can do this using Cabal or Stack. When speaking about IHP - which we are using - we just need to add the http-conduit dependency to the Haskell Packages of the App.&lt;/p&gt;

&lt;p&gt;Next, you will need the API-Key provided for your account by Bunny.net. This you can usually find in your account area:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F40gjuRK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4ahywhcnnru9nawnbpb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F40gjuRK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4ahywhcnnru9nawnbpb.png" alt="Image description" width="800" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's write a Haskell function to purge a given URL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Network.HTTP.Simple
import Network.HTTP.Types.Method (methodPost)

purgeCdnCache :: (?context :: ControllerContext) =&amp;gt; Text -&amp;gt; IO ()
purgeCdnCache url = do
  let BunnyCdnApiKey mBunnyCdnKey = getAppConfig @Config.BunnyCdnApiKey
  case mBunnyCdnKey of
    Just apiKey -&amp;gt; do
      let url' = TE.encodeUtf8 url
      let queryParams = [("url", Just url')]
      let request = setRequestMethod methodPost
                $ setRequestSecure True
                $ setRequestPort 443
                $ setRequestHost "api.bunny.net"
                $ setRequestPath "/purge"
                $ setRequestQueryString queryParams
                $ setRequestHeader "AccessKey" [TE.encodeUtf8 apiKey]
                $ defaultRequest
      _ &amp;lt;- forkIO $ do
          response &amp;lt;- httpJSON request :: IO (Response ())
          putStrLn $ "Purge request sent. Response status code: " ++ show (getResponseStatusCode response)
      pure ()
    Nothing -&amp;gt; do
      pure ()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can place this code snippet inside the Controller Helper to be able to use the purge function in any Controller of the App.&lt;/p&gt;

&lt;p&gt;This function sets up an HTTP POST request to the bunny.net purge endpoint, including the necessary AccessKey headers and Query-Parameters with the according encoding Yoga. We are forking out the request to a new Thread to not block the execution of the Controller Action. The resulting response status code from the bunny.net server is then printed out.&lt;/p&gt;

&lt;p&gt;Remember, you'll need to add the necessary imports and probably native SSL Certificate dependencies for your application (cacert).&lt;/p&gt;

&lt;p&gt;Also, keep in mind that working with external APIs might require handling various edge cases and error responses for robust production code. This example is a basic demonstration and may need to be adapted for more complex requirements or error handling.&lt;/p&gt;

&lt;p&gt;One last detail that needs to be mentioned is the environment variable for our API Key. I have modeled it to be optional since purging is not necessary in all environments of the application (only in production). The way to set up the environment variable is pretty simple and straight forward in the Config.hs file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bunnyApiKey :: Maybe Text &amp;lt;- envOrNothing "BUNNY_CDN_API_KEY"
option (BunnyCdnApiKey bunnyApiKey)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here you have it, a working CDN purge cache function! Happy purging!&lt;/p&gt;

</description>
      <category>haskell</category>
      <category>bunnycdn</category>
      <category>cdn</category>
      <category>ihp</category>
    </item>
  </channel>
</rss>
