Trending December 2023 # Learn How Does Either Works In Haskell? # Suggested January 2024 # Top 20 Popular

You are reading the article Learn How Does Either Works In Haskell? updated in December 2023 on the website Kientrucdochoi.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 Learn How Does Either Works In Haskell?

Introduction to Haskell either

In Haskell either is used to represent the possibility of two values. Either is used to represent two values that can be correct or error. It has two constructors also which are named Left and Right. These constructors also represent and show some purpose either in Haskell. Here Left constructor will show the error value, while on the other hand Right constructor will show the correct value. In the coming section of the tutorial, we will see how this function works in Haskell in more detail, and also we will see its implementation and usage while doing programming in Haskell.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

In this section we will see the syntax for either in Haskell, which is given by their official documentation, let’s have a closer look at the syntax of it for better understanding see below;

data Either a b

As you can see in the above line of syntax we have its basic definition with us. Here we are using either keyword followed by two values ‘a’ and ‘b’. Let’s have a look at the practice syntax for either, which will be more helpful to get a better idea for either in Haskell for beginners to see below;

Example:

let variable_name = Your_constructor "value if any" :: Either a basic How does either work in Haskell?

As we already know that either is used to represent two values which can be either correct or error. It also contains two constructors which further used to represent these values in Haskell either. So by the use of either, we can represent our value which is either correct or error. In this section we will first see its definition by the Haskell documentation to get a better idea, let’s get started to see below;

Method signature:

As you can see in the above line of definition for either in Haskell, is given by the Haskell official documentation. This is available inside the Data library, so we can use it as Data.eithe in Haskell. Also if we can see this closely we have two values here ‘a’ and ‘b’. These values are represented by the Left and Right constructor which will turn represent the value as correct and error. After the ‘=’ operator in the above definition, we are trying to represent the ‘a’ value by the Left constructor, and ‘b’ values is represented by the Right constructor. Below we will see the detailed description of both these constructors for better understanding see below;

Available Constructor:

1. Left: As per the official documentation of the Haskell, and as per the convention Left constructor in either is used to represent the error value. Below see the syntax to use it for better understanding see below;

Example:

Left a

1. Right: As per the official documentation of Haskell, and as per the convention Right constructor in either is used to represent the correct value. Below see the syntax to use it for better understanding see below;

Example:

Right a

1. isRight: This method is used to check whether we have passed the right value or not. It will return us the Boolean true or false. If the value if Right value then it will return true else it will return false. Below see the definition of the method as per the Haskell documentation for better understanding see below;

Example:

Now we will see one practice syntax how we can use this while programming in Haskell;

Example:

isRight (Right "your value ")

As you can see in the above line of code we are using the isRight method followed by the Right value passed inside the bracket. In this way, you can use it to evaluate the result of the passed value.

2. isLeft: This method is used to check whether we have passed the left value or not. It will return us the Boolean true or false. If the value is Left value then it will return true else it will return false. Below see the definition of the method as per the Haskell documentation for better understanding see below;

Example:

Example:

isLeft (Left "your value ")

As you can see in the above line of code we are using the isLeft method followed by the Left value passed inside the bracket. In this way, you can use it to evaluate the result of the passed value.

Examples

1. In this example we are trying to identify the value as string or int by using the either Left and Right constructor available. Remember if you want to use either while programming does not forget to import this in your program otherwise it will not work.

Example:

import Data.Either main = do print("Demo to show wokring of either in Haskell !!") print("Using either to evaluate the value either instance of string or inte using either in Haskell !!") let val1 = Left "Hello" :: Either String Int let val2 = Right 100 :: Either String Int let val3 = Left "Hellow world" :: Either String Int let val4 = Left "I m string !!" :: Either String Int let val5 = Right 900 :: Either String Int let val6 = Left "I am another string" :: Either String Int let val7 = Right 500 :: Either String Int print("Printing the result after either evalution !!") print("result one is :: ", val1) print("result two is :: ", val2) print("result three is :: ", val3) print("result four is :: ", val4) print("result five is :: ", val5) print("result six is :: ", val6) print("result seven is :: ", val7)

Output:

Conclusion

By the use of either in Haskell we can easily identify the passing value is correct or error. We also have so many methods available which can be used either to get the result fast for passing Right and Left value in Haskell. Mainly it is used to show the possibility between the two values either a or b.

Recommended Articles

We hope that this EDUCBA information on “Haskell either” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

You're reading Learn How Does Either Works In Haskell?

Learn How Untar Works In Ansible With Examples

Introduction to Ansible untar

Ansible untar module also known as unarchive for Unix OS or win_zip for the windows operating system is used to extract or unpack the archive files or folders on the remote destination by copying the archive file first by default on the remote server and also supports many parameters to deal with the unarchive or win_zip functionality like the owner, dest, exclude and more.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

For the non-windows target unarchive or ansible.builtin.unarchive command uses the below syntax. We have shown the basics parameters

Parameters:

copy: No / Yes (Default)

The default Option is Yes and it copies the files from the local controller to the remote host if specified yes or nothing specified. For working with the remote source need to specify the remote_src and this copy parameter is not helpful there.

src (required):

Source path of the file. If remote_src is ‘yes’ then the source path would be on the remote location otherwise the source path is on the local controller node.

dest (required):

Remote server absolute path where the archive is going to extract.

remote_src: no (Default) / yes

If set ‘yes’ specifies that the source archive path is on the remote computer. For the ‘no’ value the path is on the local controller node.

owner:

name of the user that should own the files/directory after unpack and same replicates (added) to the chown.

group:

Name of the group that should own the files/directory and same replicates (added) to the chown.

exclude:

List of files or directories that need to be excluded from the unpacking.

include:

List of files or directories that need to be included in the unpacking process.

keep_newer: no (default) / yes

If set yes, it replaces the existing files that are newer than the files inside the archive.

mode:

Permissions that the files or directories should have after unpacked (like 644, 777, etc).

For the windows target src, dest, creates parameters remain the same as unarchive module and the additional parameters are as below.

Parameters:

delete_archive: no (default) / yes

Removes the zip file after unzipping.

password

If the Zip file is password encrypted then this parameter is useful and it requires the PSCX module to be installed.

recurse: no (default) / yes

recursively expands the zip file within the src directory.

Setting value ‘yes’ requires the PSCX module to be installed.

Please note: the win_unzip module doesn’t use the remote_src parameter.

How untar works in Ansible?

Ansible unarchive module for non-windows target comes with the ansible-base and so it is included in the Ansible installations. For the windows target, we need to use the win_unzip module. If the module is not available then you can download it from the galaxy.

ansible-galaxy collection install community.windows

Below is the simple playbook for windows untar (unzip).

dest: c:temp7zipInstaller

Output:

The above playbook will unzip the file from the source location to the destination remote servers. If the destination path doesn’t exist it creates the destination path.

You must have noticed here, although the source path is remote, we don’t need to use remote_src because the win_unzip module doesn’t support it and when we use the remote source path for the non-target windows server we must specify the remote_src. For example,

remote_src: yes

Examples

Here are the following examples mention below

Example #1 – unarchive module task for UNIX os.

dest: /tmp/phyes

In this playbook, it will retrieve the chúng tôi file to the destination remote host /tmp/phyes. Before running this task unlike the win_unzip module, you need to make sure that the destination path exists otherwise it will throw an error.

Output:

Example #2 – Exclude files from the archive.

To exclude the certain file from the unpacking we can use h

Code:

exclude: ’Test1 document.txt’

If there are multiple files to exclude then you can provide the list as below.

- ’sourcefile.py’

Example #3 – Include files from the archive

Code:

include: ’Test1 document.txt’

The above playbook will include only the test1 text file and to include the multiple documents, use the below command.

- ’sourcefile.py’

Example #4 – using multiple parameters together.

Code:

owner: ansibleadmin

In the above playbook, it will include only 2 files, keep_newer parameter will not replace the existing files that are newer than files from the archive, remote_src indicates the remote source and it will set the owner permission on the files and it will keep the file permission 0644.

Conclusion

Untar or unarchive or unzip modules are very useful when we write the playbook. It makes it easier to extract the files or folders on the destination server without using any third-party software and in addition, it uses various parameters like we don’t need to copy the file before extracting, adding permissions after extract, etc.

Recommended Articles

This is a guide to  Ansible untar. Here we discuss How untar works in Ansible and Examples along with the codes and outputs. You may also look at the following articles to learn more –

Guide On How Does Ansible Synchronize Works

Introduction to Ansible Synchronize

Web development, programming languages, Software testing & others

Explaining the Ansible Synchronize

Ansible synchronizes work like rsync in many ways. But we must remember the below points while using it in our environments. These will explain this module and show its limitations:

To work with this module, rsync must be installed on both source and target systems.

By default, the source of files is localhost i.e. the controller machine and destination of files is the machine where the connection is made to transfer files. This default feature can be changed by using parameter delegate_to, which allows you to change your source from localhost to some other host. Thus you can copy files from a remote machine to another remote machine.

On synchronize source machine, file’s permissions are of the user who is running the tasks on localhost or remote_user in the case when delegate_to is used.

On synchronize destination machine, the file’s permission will be of remote_user on destination host or of become_user if become=yes is given in parameters, but to elevate the permission, password less sudo should be set up, as rsync itself does not give a way to pass sudo credentials.

Currently, we have only below few connection types to work with Ansible synchronize viz are ssh, paramiko, local, and docker.

Always give the full path of the destination host location, as there may be cases where you used sudo, but files will be copied to remote_user home directory.

Linux rsync limitations related to hard links are also applied here.

Ansible synchronizes module forces -delay-updates to avoid the broken state in case of connection failure.

How does Ansible Synchronize Works?

Ansible synchronize user below parameters and their acceptable values are listed as below. Using the combination of these parameters, we can decide the behavior and output of Ansible synchronize.

archive: The acceptable values are yes and no. The default value is set to yes. This mirrors the rsync flag, enable recursive, links, perm, group, owner, time flag.

hecksum: The acceptable values are yes and no. The default value is set to no. This is used to skip based on checksum.

compress: The acceptable values are yes and no. The default value is set to yes. This is used to compress files during transfer to speed up the transfer.

copy_links: The acceptable values are yes and no. The default value is set to no. This is used to copy the referenced items rather than links.

delete: The acceptable values are yes and no. The default value is set to no. This is used to delete files in dest location, which does not exist on the source somehow when the transfer is completed. This works when recursive=yes is set.

dest: The absolute or relative path on the destination machine. Which will be synced from the source.

src: The absolute or relative path on the source machine. Which will be synced from the destination.

dirs: The acceptable values are yes and no. The default value is set to no. This is used to transfer directories without recursive.

dest_port: Port number of ssh on destination.

link_dest: Default value is null. This is used to add a destination to hard links against during the rsync.

links: This is used to copy syslinks as syslinks, not referenced items.

mode: Acceptable values are push and pull. The default value is push.

owner: The acceptable values are yes and no. This is used to preserve owner.

rsync_path: This is used to specify the rsync command path on remote hosts.

times: To preserve the modification times. The acceptable values are yes and no.

Examples of Ansible Synchronize

Now by using examples, we will try to learn about Ansible synchronize, which you might have to use in day to day operations. We will take some examples, but before going there, we first understand our lab, we used for testing purpose. Here we have an Ansible control server named ansible-controller and few remote hosts named host- remote, host-one, and host-two. We will create playbooks and run Ansible commands on the ansible- controller node and see the results on the remote hosts.

In this example, we will do the synchronization of files from source machine viz. Ansible controller node to the remote host. We have a set of files under /var/tmp/sync_folder on the Ansible controller node which will be transferred to remote host via Ansible synchronize module. For this we have a playbook like below:

src: /var/tmp/sync_folder dest: /var/tmp/

ansible-playbook ansible_synchronize.yaml

ls -l

In this example, we will do the synchronization of files from one remote host to another remote host. For this we have a playbook like below:

src: /var/tmp/sync_folder dest: /var/tmp/ delegate_to: host-two

On the remote machine, we can cross-check to confirm the files have been transferred.

ls -l

Conclusion

As we saw, Ansible synchronize is powerful but easy to use the module, but we must also acknowledge that this is not the replacement of rsync in Linux systems. So keeping in mind, you must also remember all the limitations mentioned above in this article to avoid unexpected outcomes. So learn it first and then use it carefully.

Recommended Articles

This is a guide to Ansible Synchronize. Here we also discuss the Introduction and how does ansible synchronize works along with examples and its code implementation. You may also have a look at the following articles to learn more –

How Does Mongodb Unique Works With Examples

Introduction to MongoDB Unique

MongoDB’s Unique Constraint makes certain that the fields indexed in it, do not store duplicate values for the same field, i.e., making sure the uniqueness of fields. By default, MongoDB enforces this unique constraint on the “_id” field, while inserting new data. One of the major responsibilities is to ensure no duplicate data has been stored in a single key. We also have restrictions here, like MongoDB will be unable to establish a unique index on any specific field, in case if the collection somehow has already stored any data that would breach the feature of unique constraint for the index.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

Now that we have understood what Unique Constraint in MongoDB, let us learn the standard syntax to write this Unique method.

db.collection_name.createIndex( {field_name : 1} , {unqiue : true} )

How UNIQUE Constraint works in MongoDB?

The Unique Index allows the insertion and storing of the values in a document into a collection IF there is no other document in the same collection with the same index key and value. The unique constraint can be implemented on compound indexes. This helps MongoDB to implement uniqueness when combining the index key values.  Unique Constraint over separate multiple documents is possible in MongoDB, and it is an essential function in avoiding storing of same values for the indexed key in a different document.

Unique Constraint in MongoDB will allow only a single document with the same value for the indexed key. If we attempt to insert the same value for a single indexed key, it will result in an error.

Query #1

db.code.find()

This returns every document in the collection.

Query #2

db.code.insert( { name:”kumar”, city:“Pune”, code:19 } )

This is an attempt to insert a record with the same code.

Code:

db.code.find()

Output:

Explanation: Here, we have implemented the unique index for “code” key, and so when we try to store a document with existing code, it results in “duplicate key error”.

Unique Constraint for Null Value

When we store any document, we store values for the respective keys, and it is stored as per indexing. But in case, if we attempt to store a document that has no specific value or data for a key, for any uncertain key, the document will be stored, and the empty value will be stored as a null value in that document.  In MongoDB, only on the document is allowed to be stored with a single index field missing.

Examples to Implement UNIQUE Constraint in MongoDB

Implementing Unique Contraint on separate documents to not store the same value for the indexes key, in two different documents.

Example #1

We will attempt to insert and store documents with duplicate values. We have a collection named educba, which we will check with find method: db.educba.find()

Code:

db.educba.find()

Output:

As you can see in the above image, we have a collection named educba, and the document has one default unique “_id” and two more keys. We will now implement the unique constraint on the “Name”, which will make it unique, duplicate insertion proof.

Code:

db.educba.createIndex( { “Name” : 1 } , { unique : true } )

Output:

Code:

db.educba.insert({ Name : "Sulaksh" ,  City : "Pune" })

Output:

First Query:db.educba.insert({ Name : "Sulaksh" ,  City : "Pune" })

The above query will be successfully inserted.  Then we attempted to insert the same query, i.e. same document with the same values, but it resulted in an error that states, “terms”: “E11000 duplicate key error collection: test.educba index: Name_1 dup key: { : ”Sulaksh” }””. This makes our unique constraint applied over the Name field successfully.

Example #2

Code:

db.educba.createIndex( {email : 1},{unique : true} )

Output:

We will not attempt to insert a document with a different name but an email id that already exists in another document. Expectations are that the insertion operation will not work and through us an error of duplicate value. We have another document with a slightly different name: Sanket1, for the test purpose, and now we will attempt to update the document with an email: [email protected]

Code:

db.educba.update( { “Name” : ”Sanket1” } , { $set : {email : “[email protected]” }})

Above query will aim to search a record with Name: Sanket1 and will update the record with adding an email id to the document, here $set will add a field for the document.

Output:

As you can see in the above screenshot, when we attempted to update a record with the different Name field, the email was the same as one existing document. It threw us a write error, which means an error occurred while writing the document. The error states “duplicate key error collection: test. educba index: email_1 dup key”. The error points to the issue and makes things clear for us to understand.  Like in our case, the email with the value of “[email protected]” is duplicated, already stored, so it will not store again due to unique indexing on the email field. In case you want to know, I’ve used the update with $get to add another field for existing records.

Use Case for this example: Is the New User Registration where we have to ensure that a single mail id is not used to create multiple accounts.

Conclusion

Implementing Unique Constraint will restrict the database from storing duplicate values for the same indexed key to wrap it up. This feature helps in storing documents with uniqueness. We learned and understood the syntax followed by the working of it. Then we implemented the unique constraint with example and understood the same along with screenshots, respectively.

Recommended Articles

This is a guide to MongoDB Unique. Here we discuss an introduction, how does Unique works and examples for better understanding. You can also go through our other related articles to learn more –

Learn How Does Thesession Start & Ends?

Introduction to chúng tôi Session

ASP.NET Session is simply a state from which we can retrieve the user’s values and store them in the web page session. It is the way in chúng tôi to ensure that the information is passed from one page to another. Whenever the user interacts with any application on the website, firstly, it enables the start of the new session and users to do many operations like reading and writing on the websites. These values can be stored in the session object, which will be stored on the server side. Also, for that particular user, it can use that value on any page on the same website. So, every value has a unique ID.

Start Your Free Software Development Course

When does the chúng tôi Session Start?

We all must have a basic question when does it exactly start? So, it will begin when we hit the website. The chúng tôi session will be generated as soon as the first request has been made to the web application. By default, the session state is in Process. The session starts when a user hits any website or application for the first time. If the user is inactive or doesn’t do anything longer on the same page, the server places the session memory allocated for the user. Now, if the user hits the page after the session expiration again, a new session will be created and assigned for the user request. As it is server-side functionality, Session variables are not accessible by the website user.

Let’s have a look at how chúng tôi Session gets started in below four ways:

1. When a user sends or requests for a URL that identifies an chúng tôi File in that application. Session_OnStart event procedure is included in chúng tôi File for that application.

2. The session object is used to store the user values.

3. Whenever the server receives a request with no valid SessionID cookie, a new session gets started.

When a new user visits or hits the application website, we can say that Session_Start is fired.

When does the chúng tôi Session End?

We have seen when exactly the chúng tôi session starts; let’s see when exactly it ends.

Many of us experience this scenario of session end. When we are on a web page of an application for a longer time, and we have not requested or refreshed that page for a longer duration, then the session will end automatically.

When the web server collapses, the user session may get ended.

By default, the value for the timeout is 20 minutes.

We can manually set it as a session if we want to reduce session timeout. Timeout Property.

We can set its value according to our requirements for the web application. A shorter session timeout will help to reduce the strain on our server’s memory resources.

Also, if we want to close or end the session purposely, we can use the method Session. Abandon of Session Object.

Sessions that do not have any state will automatically end. Due to the stateless session, its session object doesn’t contain any content or static object.

Examples to Implement chúng tôi Session

Let’s take an example of a Session that will give us a better understanding. This example contains creating a new session and storing its mail id.

1. Default.aspx

This chúng tôi code will allow the user to design its website or application by designing the user interface.

Code:

<%@ Page Title="Home Page"Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" .auto-style1 { width: 100%; } .auto-style2 { width: 105px; } 2. Default.aspx.cs

Default.aspx.cs will allow the developer to write their C# code to develop the website of an application.

Code:

usingSystem; usingWeb.UI; namespaceSessionExample { public partial class _Default : Page { { if (password.Text=="edu123") { Session["email"] = email.Text; } if(Session["email"] != null) { Text = "Email is stored to the session."; Text = Session["email"].ToString(); } } } }

Output:

In the above snippet, it is evident that the session stores the email “[email protected]”

Conclusion

We have learned about chúng tôi sessions in this article. I hope now it is very clear about when the chúng tôi Session gets started and ends. With the help of an example, it is clear what exactly the session is and how it deals with any web application. chúng tôi web application will, by default, create a session for data storage for users.

Recommended Articles

We hope that this EDUCBA information on “ASP.NET Session” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

Learn How Does Azure Monitor Work With Its Types?

Introduction to Azure monitor

Hadoop, Data Science, Statistics & others

What is an Azure monitor?

The Azure monitor supports the user to achieve maximum performance in services and applications. As discussed above, the below points stress the necessity and application of Azure monitor. It diagnoses and detects the bug in any application with its feature called application insights. It can compare the infrastructure flaws with container and VM insights. Then it can penetrate the application and monitor data with analytical log details to troubleshoot and work on deep insights. It supports operations at a finite scale by using its automated actions and smart alert systems. Finally, it can develop visualization with workbooks and Azure dashboards.

How does Azure monitor work?

The infrastructure of the Azure monitor has a high-level view. The metrics and logs are placed at the center of the Azure monitor, which works on two important types of information used by the Azure monitor. In the right of the Azure monitor, there are resources to work on the data monitoring events which populate the data stores. On the other end, Azure monitor work on different functions which work on the collected data. It also performs alerting, analysis, streaming, and other actions to its external connected systems.

In simple, it can be termed as: Azure monitor gathers the information from different sources like operating systems, applications, various resources of Azure in the formats of logs and metrics. It can then be processed to work on different functions like visualization, automation, integration, alert systems, and deep analysis.

Metrics are coined as a measurement to view aspects of resources. For example, it helps to check on the number of connections, disk IOPS, CPU utilization, swap space, memory utilization, etc. It is real-time and saved as a finite value with a periodic collection interval. Therefore, it is adapted to the environment better, and the collected logs are displayed as graphs concerning time and performance.

Logs are a collection of records with metadata and different properties that have a simple structure and provide an organized view of the SQL database. It is comprised of numerical information like metrics which has brief text with extra information. Traces and events are the best examples of logs. It is a dash of data that periodically collects the data which needs to be manipulated to view. The user wants to extract and filter the data from the logs by using a native query called kusto query language. It can be used to produce some logical visualization, and it can be pinned to dashboards.

Azure Monitor types collect

Application data: The information about the compiled code’s functionality and performance irrespective of any platform is collected from the application monitoring data.

Operating System data: The information about the operation system on which the user is hosting the application is collected in Guest monitoring OS data. It can be another cloud, Azure, or on-premise.

Resources on Azure data: The information about the process of an Azure resource is collected in Azure resource monitoring events.

The subscription on Azure data: Azure’s proper health check process manages the information about the management and operation of subscriptions in Azure.

Key requirements

The alert system is one of the key requirements for Azure monitor. It proactively alerts the user when the application is facing critical conditions and requires some corrective actions. The alert rules depend on metrics provided in real-time based on numerical values. In addition, it is based on logs which enable solving complex logic across the data from multiple resources.

Azure monitor application insights

Monitoring the information is useful only if it enhances the visibility of the process in the computing environment. The insights offer a customized monitoring experience for a few particular services. It provides minimal configuration and enhances visibility in the area of critical resources. Application insight always monitors the performance, availability, usage, swap space, and memory allocation of the web application deployed on on-premise or cloud. It structures the powerful data in the Azure platform to offer the user a deep view of the application’s performance. It allows the user to detect errors without any delay and report them to him. The application insight enables different points in development tools and combines them with visual studio to support the process in DevOps.

Azure Monitor Visualizing monitoring data

The visualization in Azure monitor offers tables and charts as effective tools for succinct the monitoring data to display it to a different set of audience. In addition, it has its features to visualize the monitoring data and structure the services of Azure to telecast to the audience. Apart from charts and tables, the monitor has attractive dashboards, workbooks, and power BI to consolidate the performance of the application and display it to the user for better understandings.

Conclusion

Once the Azure subscription is activated, the azure monitor performs and monitors all the addition of resources like web applications and virtual machines. The Azure monitoring begins to collect the data, and the activity logs are recorded when all the resources are developed or edited. The metrics explain the performance of the resources. The diagnostics enable options to allow the user to maximize the availability of the data, and the agent addition option is there to compute the resources to gather info from telemetry from guest OS.

Recommended Articles

This is a guide to Azure monitor. Here we discuss how Azure monitors work and the key requirement and types collected. You may also have a look at the following articles to learn more –

Update the detailed information about Learn How Does Either Works In Haskell? on the Kientrucdochoi.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!