Found 26504 Articles for Server Side Programming

Haskell Program to Calculate the Logarithm Gamma of the Given Number

Akhil Sharma
Updated on 28-Mar-2023 12:20:36

149 Views

In Haskell, we will calculate the logarithm gamma of the given number by using Stirling’s approximation and Lanczos appromixation formula. In the first example, we are going to use Stirling’s approximation with (s = foldr (\(c, q) acc -> c + (q / (x + acc))) 0 (zip (tail p) q) in (log s) - t + log (sqrt (2 * pi) / x) + (c * log (1 + c / 12.0 - (c * c) / 360.0)) function and in the second example, we are going to use Lanczos approximation formula along with (lanczos = log $ ... Read More

Haskell Program to Round a Number to n Decimal Places

Akhil Sharma
Updated on 28-Mar-2023 11:58:06

2K+ Views

In Haskell, we can use round, printf and truncate functions to round a number to n decimal places. In the first example, we are going to use (roundTo n x = (fromInteger $ round $ x * (10^n)) / (10.0^^n)) function and in the second example, we are going to use (roundTo n x = read $ printf ("%." ++ show n ++ "f") x) function.In the third example, we are going to use (roundTo n x = fromIntegral (truncate $ x * 10^n) / 10^n). Algorithm Step 1 − The roundTo function is defined using round function Step ... Read More

How to test the equality of Swift enums with associated values?

Nitin Aggarwal
Updated on 11-Apr-2023 10:41:59

2K+ Views

In Swift, you can use the Equatable protocol to compare enums with associated values. In this article, we will see how we can compare them using the Equatable protocol with an example. Enumeration with Associated Values In Swift, you can provide a value along with an enum case. This makes enumeration more powerful in Swift. A Swift feature called an enumeration with associated values enables you to design a type that can have a finite set of cases, each of which can have a unique set of associated values of any type. This enables you to link data to each ... Read More

How to return the first 5 objects of an array in Swift?

Nitin Aggarwal
Updated on 04-Apr-2023 10:23:43

4K+ Views

In Swift to get the first N objects of an array, we can use the prefix function or Range operator. This prefix function is used to retrieve the prefix elements by passing the count limit. Also, you can use the range operator to get n number of elements from an array. Let's see some examples. In Swift, the prefix function returns an array containing the first n elements. Using the Prefix Function You can use the prefix function to get the first n elements of an array. Step 1 − Create an input array Step 2 − Call the ... Read More

How to iterate for loop in reverse order in Swift?

Nitin Aggarwal
Updated on 04-Apr-2023 10:41:25

6K+ Views

In Swift, there are different approaches to iterating a for loop in reverse order. In this article, you will see some methods like reverse, ranges, stride(from:to:by:), forEach(), etc. Each method can be used in different use cases. Also, you should know that each method has its own performance. Using the reversed() Method This method returns a new array by reversing the elements of an array. It does not change the order of the input array. Step 1 − Create an input array to iterate Step 2 − Perform a for loop with the reversed() function Step 3 − Perform an ... Read More

How to copy text to the clipboard/pasteboard with Swift?

Nitin Aggarwal
Updated on 04-Apr-2023 10:44:36

3K+ Views

In Swift, there is a dedicated class called UIPasteboard that allows you to copy text to the pasteboard. The same class allows you to paste the text. A UIPasteboard class is part of the UIKit framework that provides a general processor for copying and pasting information in iOS applications. In this class, data can be copied and pasted between apps using a shared instance. You can share various types of information such as text, media files, URLs, and colors. Copy and Paste the Text to the Clipboard Using the UIPasteboard class, you can copy and paste the text value in ... Read More

How to convert float to int in Swift?

Nitin Aggarwal
Updated on 04-Apr-2023 10:45:51

3K+ Views

In Swift, you can convert a float to an Int by using the Int() constructor. This will round the float value toward zero and return an integer value. Remember that, this constructor returns an optional value, so you have to use an optional binding approach to wrap the value safely. In this article, you will see some examples of how to use the Int() constructor. Using the Int() constructor You can use the Int() constructor to convert a Float to an Int. This method rounds the float value toward zero and returns an integer value. Step 1 − Declare ... Read More

How to convert a number to an absolute value in Swift?

Nitin Aggarwal
Updated on 04-Apr-2023 11:26:50

3K+ Views

To convert a number to its absolute value in Swift, you can use the abs(_:) function. In this article, you will see multiple examples of how to use the abs() function in the Swift language. Example 1 In this example, you will convert a simple negative number to an absolute value. Step 1 − Declare an input variable with an initial numeric value Step 2 − Convert the numeric input value into absolute value using the abs() function Step 3 − Assign the output absolute value to a new variable Step 4 − Print the input value to the ... Read More

How to convert a JSON string to a dictionary in Swift?

Nitin Aggarwal
Updated on 04-Apr-2023 11:30:01

7K+ Views

Swift provides a class called JSONSerialization to convert a JSON string to dictionary format. For example, you are receiving a JSON string from the database, in order to use it in the application, you might need to convert it into a real object type. In this article, you will see some examples of how to convert a JSON string into a dictionary. What is JSON String? A JSON string is a string that is converted to a different format such as base64 or URL encoding. This converted string can be used for networking requests or to store in the database. ... Read More

How do I make an enum decodable in Swift?

Nitin Aggarwal
Updated on 04-Apr-2023 11:31:34

1K+ Views

In Swift, you can use the Codable protocol to make an enum decodable as well. In Swift, the Codable protocol is a very powerful concept to provide flexibility to decode and encode any type of value. Also, you can make an enum decodable as well. You have to just define an enum in your custom type. In order to make an enum decodable, the enum must conform to the Codable protocol. In this article, we will learn how to use the Codable protocol to make an enum decodable in Swift What is the JSONDecoder Class? The JSONDecoder class is then ... Read More

Advertisements