### Installing Pratica Library Source: https://github.com/rametta/pratica/blob/master/README.md This snippet provides commands to install the Pratica functional programming library using popular package managers like Bun, Yarn, and npm. ```Shell bun i pratica # or yarn add pratica # or npm i pratica ``` -------------------------------- ### Swapping Ok and Err Types with Result.swap in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md The `swap` method converts an `Ok` type to an `Err` type, and an `Err` type to an `Ok` type, effectively flipping the success/failure state. The encapsulated value is preserved during the swap. This example shows an `Ok` value being swapped to an `Err` and then handled by the `cata`'s `Err` branch. ```JavaScript import { Ok } from "pratica" Ok("hello") .swap() .cata({ Ok: () => console.log(`doesn't run`), Err: (x) => expect(x).toBe("hello"), // true }) ``` -------------------------------- ### Applying Functions to Result Values with Result.ap (JavaScript) Source: https://github.com/rametta/pratica/blob/master/README.md The `.ap()` method is used to apply a function wrapped in an `Ok` to a value also wrapped in an `Ok`. This enables combining multiple `Ok` values, similar to an applicative functor. The example shows how to apply a curried function to multiple `Ok` values and also demonstrates how `ap` handles cases where the initial `Ok` does not contain a function. ```javascript import { Ok } from "pratica" // Need something like this // Ok(6) + Ok(7) = Ok(13) Ok((x) => (y) => x + y) .ap(Ok(6)) .ap(Ok(7)) .cata({ Ok: (result) => console.log(result), // 13 Err: () => console.log(`This function won't run`), }) Ok(null) // no function to apply .ap(Ok(6)) .ap(Ok(7)) .cata({ Ok: () => console.log(`This function won't run`), Err: () => console.log(`This function runs`), }) ``` -------------------------------- ### Safely Getting the Tail of an Array in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Retrieves all elements of an array except the first one, returning a `Maybe` type. It returns `Just` with the remaining elements if the array is not empty, or `Nothing` if the array is empty, providing a safe way to get a sub-array. It takes an array as input. ```javascript import { tail } from "pratica" const data = [5, 1, 2] // example with data tail(data).cata({ Just: (x) => expect(x).toEqual([1, 2]), // true, Nothing: () => console.log("No tail") // won't run }) // example with empty data tail([]).cata({ Just: (x) => console.log(x), // doesn't run Nothing: () => console.log("No tail") // runs }) ``` -------------------------------- ### Checking for Nothing Type with Maybe.isNothing in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md The `isNothing` method returns a boolean indicating if the `Maybe` instance is a `Nothing` type. It returns `true` for `Nothing` and `false` for `Just`. This example demonstrates its usage to check the type of a `Maybe` value derived from a height comparison. ```JavaScript import { Just, Nothing } from "pratica" const isOver6Feet = (height) => (height > 6 ? Just(height) : Nothing) isOver6Feet(7).isNothing() // false isOver6Feet(4).isNothing() // true ``` -------------------------------- ### Modifying Ok and Err Values with Result.bimap (JavaScript) Source: https://github.com/rametta/pratica/blob/master/README.md The `.bimap()` method allows for simultaneously transforming both the `Ok` and `Err` values of a `Result` monad. It acts as a shorthand for applying separate `map` and `mapErr` operations. This example demonstrates how `bimap` applies the first function to an `Ok` value and the second function to an `Err` value, verifying the transformations with `expect`. ```javascript import { Ok } from "pratica" Ok("hello") .bimap( (x) => x + " world", (x) => x + " goodbye", ) .cata({ Ok: (x) => expect(x).toBe("hello world"), // true Err: () => {}, }) Err("hello") .bimap( (x) => x + " world", (x) => x + " goodbye", ) .cata({ Ok: () => {}, Err: (x) => expect(x).toBe("hello goodbye"), // true }) ``` -------------------------------- ### Safely Getting the First Array Element in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Retrieves the first element of an array, returning a `Maybe` type. It returns `Just` if the array is not empty, containing the first element, or `Nothing` if the array is empty, providing a safe way to access array elements without direct null checks. It takes an array as input. ```javascript import { head } from "pratica" const data = [5, 1, 2] // example with data head(data).cata({ Just: (x) => expect(x).toBe(5), // true, Nothing: () => console.log("No head") // won't run }) // example with empty data head([]).cata({ Just: (x) => console.log(x), // doesn't run Nothing: () => console.log("No head") // runs }) ``` -------------------------------- ### Retrieving Encapsulated Value with Maybe.value in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md The `value` method returns the encapsulated value within a `Maybe` instance. If the `Maybe` is a `Just` type, its contained value is returned. If it is a `Nothing`, `undefined` is returned. This example illustrates how to retrieve the value or `undefined` based on the `Maybe` type. ```JavaScript import { Just, Nothing } from "pratica" const isOver6Feet = (height) => (height > 6 ? Just(height) : Nothing) isOver6Feet(7).value() // 7 isOver6Feet(4).value() // undefined ``` -------------------------------- ### Checking for Just Type with Maybe.isJust in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md The `isJust` method returns a boolean indicating if the `Maybe` instance is a `Just` type. It returns `true` for `Just` and `false` for `Nothing`. This example demonstrates its usage to check the type of a `Maybe` value derived from a height comparison. ```JavaScript import { Just, Nothing } from "pratica" const isOver6Feet = (height) => (height > 6 ? Just(height) : Nothing) isOver6Feet(7).isJust() // true isOver6Feet(4).isJust() // false ``` -------------------------------- ### Sequencing Operations with Result.chain in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md The `chain` method (also known as `flatMap`) allows sequencing multiple operations that return `Result` types. If any operation in the chain returns an `Err`, subsequent operations are skipped, and the `Err` propagates. This is useful for simplifying complex conditional logic. This example demonstrates chaining multiple validation functions on a person object. ```JavaScript import { Ok, Err } from "pratica" const person = { name: "Jason", age: 4 } const isPerson = (p) => (p.name && p.age ? Ok(p) : Err("Not a person")) const isOlderThan2 = (p) => (p.age > 2 ? Ok(p) : Err("Not older than 2")) const isJason = (p) => (p.name === "jason" ? Ok(p) : Err("Not jason")) Ok(person) .chain(isPerson) .chain(isOlderThan2) .chain(isJason) .cata({ Ok: (p) => console.log("this person satisfies all the checks"), Err: (msg) => console.log(msg), // if any checks return an Err, then this function will be called. If isPerson returns Err, then isOlderThan2 and isJason functions won't even execute, and the err msg would be 'Not a person' }) ``` -------------------------------- ### Transforming Ok Value with Result.map in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md The `map` method on a `Result` type transforms the encapsulated value if the `Result` is an `Ok`. The provided function is applied to the `Ok` value, and a new `Ok` containing the transformed value is returned. If the `Result` is an `Err`, the `map` operation is skipped. This example shows mapping a person object to their name. ```JavaScript import { Ok, Err } from "pratica" const person = { name: "jason", age: 4 } Ok(person) .map((p) => p.name) .cata({ Ok: (name) => console.log(name), // 'jason' Err: (msg) => console.error(msg), // this func does not run }) ``` -------------------------------- ### Extracting Value with Maybe.cata in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md The `cata` method is used at the end of a computation chain to extract the final value from a `Maybe` type. It requires an object with `Just` and `Nothing` properties, both of which must be functions. The appropriate function is executed based on whether the `Maybe` instance is `Just` or `Nothing`. This example demonstrates how `cata` handles a `Nothing` value, preventing the `Just` function from running. ```JavaScript import { Just, Nothing } from "pratica" const isOver6Feet = (person) => (person.height > 6 ? Just(person.height) : Nothing) isOver6Feet({ height: 4.5 }) .map((h) => h / 2.2) .cata({ Just: (h) => console.log(h), // this function doesn't run Nothing: () => console.log(`person is not over 6 feet`), }) ``` -------------------------------- ### Sequencing Error Transformations with Result.chainErr in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md The `chainErr` method allows sequencing operations that return `Err` types, similar to `chain` but for the error path. It transforms the error value if the `Result` is an `Err`. This example demonstrates how `chainErr` can be used to modify an error message before it is finally handled. ```JavaScript import { Err } from "pratica" Err("Message:") .chainErr((x) => x + Err(" Syntax Error")) .map((x) => x + 7) // ignored because it's an error .cata({ Ok: (x) => console.log(x), // function not ran Err: (x) => console.log(x), // 'Message: Syntax Error' }) ``` -------------------------------- ### Transforming Error Value with Result.mapErr in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md The `mapErr` method transforms the encapsulated error value if the `Result` is an `Err`. The provided function is applied to the `Err` value, and a new `Err` containing the transformed error is returned. If the `Result` is an `Ok`, the `mapErr` operation is skipped. This example shows appending a string to an error message. ```JavaScript import { Err } from "pratica" Err("Message:") .mapErr((x) => x + " Syntax Error") .map((x) => x + 7) // ignored because it's an error .cata({ Ok: (x) => console.log(x), // function not ran Err: (x) => console.log(x), // 'Message: Syntax Error' }) ``` -------------------------------- ### Safely Getting the Last Array Element in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Retrieves the last element of an array, returning a `Maybe` type. It returns `Just` if the array is not empty, containing the last element, or `Nothing` if the array is empty, providing a safe way to access array elements without direct null checks. It takes an array as input. ```javascript import { last } from "pratica" const data = [5, 1, 2] // example with data last(data).cata({ Just: (x) => expect(x).toBe(2), // true, Nothing: () => console.log("No last") // won't run }) // example with empty data last([]).cata({ Just: (x) => console.log(x), // doesn't run Nothing: () => console.log("No last") // runs }) ``` -------------------------------- ### Handling Result Outcomes with Result.cata (JavaScript) Source: https://github.com/rametta/pratica/blob/master/README.md The `.cata()` method is a pattern-matching function that allows handling both `Ok` and `Err` states of a `Result` monad by providing an object with `Ok` and `Err` properties, each mapping to a callback function. This enables branching logic based on the `Result`'s state, ensuring all possible outcomes are explicitly addressed. The example demonstrates how `cata` executes the appropriate callback based on whether the `isOver6Feet` function returns an `Ok` or an `Err`. ```javascript import { Ok, Err } from "pratica" const isOver6Feet = (person) => (person.height > 6 ? Ok(person.height) : Err("person is not over 6 feet")) isOver6Feet({ height: 4.5 }) .map((h) => h / 2.2) .cata({ Ok: (h) => console.log(h), // this function doesn't run Err: (msg) => console.log(msg), // `person is not over 6 feet` }) ``` -------------------------------- ### Providing Default Value with Maybe.alt in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Shows how `Maybe.alt` ensures a `Just` value is always returned, providing a default value if the current Maybe is `Nothing`, useful for fallback scenarios. ```JavaScript import { nullable } from "pratica" // Example with default data nullable(null) .map((p) => p.age) // won't run .map((age) => age + 5) // won't run .alt(99) // the data is null so 99 is the default .cata({ Just: (age) => console.log(age), // 99 Nothing: () => console.log(`This function won't run because .alt() always returns a Just`) }) ``` -------------------------------- ### Applying Functions with Maybe.ap in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Demonstrates `Maybe.ap`, which simplifies applying a function wrapped in a Maybe to a value wrapped in another Maybe, avoiding repetitive `.map` calls for multiple arguments. ```JavaScript import { Just, nullable } from "pratica" // Need something like this // Just(6) + Just(7) = Just(13) Just((x) => (y) => x + y) .ap(Just(6)) .ap(Just(7)) .cata({ Just: (result) => console.log(result), // 13 Nothing: () => console.log(`This function won't run`) }) nullable(null) // no function to apply .ap(Just(6)) .ap(Just(7)) .cata({ Just: () => console.log(`This function won't run`), Nothing: () => console.log(`This function runs`) }) ``` -------------------------------- ### Converting Result to Maybe with Result.toMaybe (JavaScript) Source: https://github.com/rametta/pratica/blob/master/README.md The `.toMaybe()` method converts a `Result` monad into a `Maybe` monad. An `Ok` value becomes a `Just` with the same content, while an `Err` value becomes a `Nothing`. This conversion is useful when you want to abstract away the error message and only care about the presence or absence of a value. ```javascript import { Ok, Err } from "pratica" Ok(8) .toMaybe() .cata({ Just: (n) => console.log(n), // 8 Nothing: () => console.log(`No value`), // this function doesn't run }) Err(8) .toMaybe() .cata({ Just: (n) => console.log(n), // this function doesn't run Nothing: () => console.log(`No value`), // this runs }) ``` -------------------------------- ### Chaining Maybes with Maybe.chain in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Illustrates `Maybe.chain`, used when a function inside a Maybe needs to return another Maybe. It short-circuits subsequent operations if an intermediate Maybe becomes `Nothing`. ```JavaScript import { nullable } from "pratica" const person = { name: "Jason", age: 4 } nullable(person) .chain((p) => nullable(p.height)) // p.height does not exist so nullable returns a Nothing type, any .map, .chain, or .ap after a Nothing will be short circuited .map((height) => height * 2.2) // this func won't even run because height is Nothing, so `undefined * 2.2` will never execute, preventing problems. .cata({ Just: (height) => console.log(height), // this function won't run because the height is Nothing Nothing: () => console.log("This person has no height") }) ``` -------------------------------- ### Converting Maybe to Result with Maybe.toResult in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md The `toResult` method converts a `Maybe` type to a `Result` type. A `Just` value is converted to an `Ok` with the same encapsulated value, while `Nothing` is converted to an `Err` with no value. The `cata` method then uses `Ok` and `Err` properties instead of `Just` and `Nothing` to handle the outcome. This snippet shows conversions for both `Just` and `Nothing` cases. ```JavaScript import { Just, Nothing } from "pratica" Just(8) .toResult() .cata({ Ok: (n) => console.log(n), // 8 Err: () => console.log(`No value`), // this function doesn't run }) Nothing.toResult().cata({ Ok: (n) => console.log(n), // this function doesn't run Err: () => console.log(`No value`), // this runs }) ``` -------------------------------- ### Applying Function with Maybe.map in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Demonstrates the `Maybe.map` method, which applies a function to the wrapped data if the Maybe is `Just`, or short-circuits if it's `Nothing`, preventing errors from null or undefined values. ```JavaScript import { nullable } from "pratica" const person = { name: "Jason", age: 4 } // Example with real data nullable(person) .map((p) => p.age) .map((age) => age + 5) .cata({ Just: (age) => console.log(age), // 9 Nothing: () => console.log(`This function won't run`) }) // Example with null data nullable(null) .map((p) => p.age) // Maybe type is Nothing, so this function is skipped .map((age) => age + 5) // Maybe type is Nothing, so this function is skipped .cata({ Just: (age) => console.log(age), // Maybe type is Nothing, so this function is not run Nothing: () => console.log("Could not get age from person") // This function runs because Maybe is Nothing }) ``` -------------------------------- ### Checking for Ok State with Result.isOk (JavaScript) Source: https://github.com/rametta/pratica/blob/master/README.md The `.isOk()` method returns a boolean indicating whether the `Result` monad currently holds an `Ok` value. This is a simple way to check the success state without needing to use `cata` or `map`. ```javascript import { Ok, Err } from "pratica" const isOver6Feet = (height) => (height > 6 ? Ok(height) : Err("Shorty")) isOver6Feet(7).isOk() // true isOver6Feet(4).isOk() // false ``` -------------------------------- ### Safely Executing Functions with encaseRes (JavaScript) Source: https://github.com/rametta/pratica/blob/master/README.md The `encaseRes` utility function safely executes a given function, returning a `Result` monad. If the function executes successfully, it returns an `Ok` containing the function's return value. If the function throws an error, it returns an `Err` containing the error message, providing more context than `encase` which returns `Nothing`. This is ideal for scenarios where the error details are important. ```javascript import { encaseRes } from "pratica" const throwableFunc = () => JSON.parse("<>") // this func doesn't throw, so Ok is called encaseRes(() => "hello").cata({ Ok: (x) => console.log(x), // hello Err: () => console.log("func threw error"), // this func doesn't run }) // this function throws an error so Err is called encaseRes(throwableFunc).cata({ Ok: (json) => console.log(`doesn't run`), Err: (msg) => console.error(msg), // SyntaxError: Unexpected token < in JSON at position 0 }) ``` -------------------------------- ### Inspecting Maybe Values with Maybe.inspect in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Illustrates `Maybe.inspect`, a utility method that returns a string representation of the Maybe's current state (`Just(value)` or `Nothing`), primarily for debugging and logging. ```JavaScript import { nullable } from "pratica" nullable(86).inspect() // `Just(86)` nullable("HELLO").inspect() // `Just('HELLO')` nullable(null).inspect() // `Nothing` nullable(undefined).inspect() // `Nothing` ``` -------------------------------- ### Safely Parsing Dates with parseDate (JavaScript) Source: https://github.com/rametta/pratica/blob/master/README.md The `parseDate` utility function safely attempts to parse a date string, returning a `Maybe` monad. If the parsing is successful, it returns a `Just` containing the `Date` object; otherwise, it returns a `Nothing`. This prevents exceptions from invalid date strings and allows for functional handling of parsing outcomes, including chaining with other `Maybe` methods like `default`. ```javascript import { parseDate } from "pratica" const goodDate = "2019-02-13T21:04:10.984Z" const badDate = "2019-02-13T21:04:1" parseDate(goodDate).cata({ Just: (date) => expect(date.toISOString()).toBe(goodDate), Nothing: () => console.log("could not parse date string"), // this function doesn't run }) parseDate(badDate).cata({ Just: () => console.log(`this function doesn't run`), Nothing: () => "this function runs", }) // it's a maybe, so you can use chain/default/ap parseDate(null) .default(() => new Date()) .cata({ Just: (date) => date.toISOString(), // this runs Nothing: () => `doesn't run because of the .default()`, }) ``` -------------------------------- ### Safely Executing Functions with encase (JavaScript) Source: https://github.com/rametta/pratica/blob/master/README.md The `encase` utility function safely executes a given function, wrapping its result in a `Maybe` monad. If the function executes without throwing an error, `encase` returns a `Just` containing the function's return value. If the function throws an error, it returns a `Nothing`, preventing the error from propagating and allowing for graceful error handling. ```javascript import { encase } from "pratica" const throwableFunc = () => JSON.parse("<>") // this func doesn't throw, so Just is called encase(() => "hello").cata({ Just: (x) => console.log(x), // hello Nothing: () => console.log("func threw error"), // this func doesn't run }) // this function throws an error so Nothing is called encase(throwableFunc).cata({ Just: (json) => console.log(`doesn't run`), Nothing: () => console.error("func threw an error"), // this runs }) ``` -------------------------------- ### Collecting Values from an Array of Maybes in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Aggregates values from an array of `Maybe` types into a single `Maybe` type. If all elements are `Just`, it returns a `Just` containing an array of the unwrapped values. If any element is `Nothing`, it returns `Nothing`, providing a way to handle multiple optional operations. It takes an array of `Maybe` types as input. ```javascript import { collectMaybe } from "pratica" const all_good = [Just(1), Just(2), Just(3)] const one_bad = [Just(1), Nothing, Just(3)] collectMaybe(all_good).cata({ Just: (x) => expect(x).toEqual([1, 2, 3]), // true Nothing: () => "no values" // doesn't run }) collectMaybe(one_bad).cata({ Just: (x) => x, // doesn't run Nothing: () => "no values" // true }) ``` -------------------------------- ### Safely Finding an Item in an Array in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Attempts to find an item in an array based on a provided predicate function, returning a `Maybe` type. It returns `Just` with the found item if a match is found, or `Nothing` if no item satisfies the predicate, providing a safe search mechanism. It takes a predicate function and an array as input. ```javascript import { tryFind } from "pratica" const users = [ { name: "jason", age: 6, id: "123abc" }, { name: "bob", age: 68, id: "456def" } ] tryFind((u) => u.id === "123abc")(users).cata({ Just: (user) => expect(user).toEqual(users[0]), // true Nothing: () => "Could not find user with id 123abc" // doesn't run }) ``` -------------------------------- ### Inspecting Result Values with Result.inspect (JavaScript) Source: https://github.com/rametta/pratica/blob/master/README.md The `.inspect()` method provides a string representation of the `Result` monad, indicating whether it's an `Ok` or an `Err` and displaying its contained value. This is useful for debugging and logging the state of a `Result` instance. ```javascript import { Ok, Err } from "pratica" Ok(86).inspect() // `Ok(86)` Ok("HELLO").inspect() // `Ok('HELLO')` Err("Something happened").inspect() // `Err('Something happened')` Err(404).inspect() // `Err(404)` ``` -------------------------------- ### Safely Retrieving Nested Properties with Pratica in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Safely retrieves a nested property from an object using a path array. This function returns a `Maybe` type, which can be `Just` if the property is found, or `Nothing` if any part of the path is undefined, preventing runtime errors. It takes a path array and the target object as input. ```javascript import { get } from "pratica" const data = { name: "jason", children: [ { name: "bob" }, { name: "blanche", children: [ { name: "lera" } ] } ] } get(["children", 1, "children", 0, "name"])(data).cata({ Just: (name) => expect(name).toBe("lera"), // true Nothing: () => console.log("no name") // doesn't run }) ``` -------------------------------- ### Collecting Values from an Array of Results in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Aggregates values from an array of `Result` types into a single `Result` type. If all elements are `Ok`, it returns an `Ok` containing an array of the unwrapped values. If any element is an `Err`, it returns the first `Err` encountered, providing a way to handle multiple fallible operations. It takes an array of `Result` types as input. ```javascript import { collectResult } from "pratica" const all_good = [Ok(1), Ok(2), Ok(3)] const one_bad = [Ok(1), Err("Some error"), Ok(3)] collectResult(all_good).cata({ Ok: (x) => expect(x).toEqual([1, 2, 3]), // true Err: () => "no values" // doesn't run }) collectResult(one_bad).cata({ Ok: (x) => x, // doesn't run Err: (err) => expect(err).toEqual("Some error") // true }) ``` -------------------------------- ### Filtering Ok Types in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Filters an array to return only elements wrapped in an `Ok` data type. This function is useful for extracting successful results from a mixed array of `Result` types, ensuring type safety and functional purity. It takes an array as input and returns a new array containing only the `Ok` instances. ```javascript import { oks } from "pratica" const data = [1, true, Just("hello"), Nothing, Ok("hey"), Err("No good")] oks(data) // returns [Ok('hey')] ``` -------------------------------- ### Extracting Value from Result.value (JavaScript) Source: https://github.com/rametta/pratica/blob/master/README.md The `.value()` method directly returns the contained value of an `Ok` or `Err` instance. While convenient, it bypasses the monadic safety, meaning it will throw an error if called on an `Err` when an `Ok` is expected, or vice-versa, if not handled carefully. It's primarily used for direct access when the `Result` type is known. ```javascript import { Ok, Err } from "pratica" const six = Ok(6).value() const error = Err("Something happened").value() console.log(six) // 6 console.log(error) // 'Something happened' ``` -------------------------------- ### Filtering Just Types in JavaScript Source: https://github.com/rametta/pratica/blob/master/README.md Filters an array to return only elements wrapped in a `Just` data type. This function is useful for extracting successful or present values from a mixed array, ensuring type safety and functional purity. It takes an array as input and returns a new array containing only the `Just` instances. ```javascript import { justs } from "pratica" const data = [1, true, Just("hello"), Nothing, Ok("hey"), Err("No good")] justs(data) // returns [Just('hello')] ``` -------------------------------- ### Checking for Err State with Result.isErr (JavaScript) Source: https://github.com/rametta/pratica/blob/master/README.md The `.isErr()` method returns a boolean indicating whether the `Result` monad currently holds an `Err` value. This provides a straightforward way to determine if an error occurred without needing to deconstruct the `Result`. ```javascript import { Ok, Err } from "pratica" const isOver6Feet = (height) => (height > 6 ? Ok(height) : Err("Shorty")) isOver6Feet(7).isErr() // false isOver6Feet(4).isErr() // true ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.