clj-commons / clj-commons/failjure

Oposite of try-all: try-all-failed and similar

Open
#36 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Clojure
Stars
427
Forks
20
PR merge metrics
No merged PRs in 30d

Description

I think `try-all-failed ` and similar might also be useful.

I am trying to parse some input from user (a time Duration) and trying to be forgiving.
In my case I need to try out all options and stop when I get a non-failure.

So my algorithm is :

- try to parse user input
- if failed try to parse user input with "PT" pre-pended
- if failed try to parse user input with "P" pre-pended
- give up with parsing
- or if one did not fail, return that value

I think code would look like this:
(beginner with Failjure, code might not be correct)

```clojure
(try-all-failed [duration (try-parse s)
duration (try-parse (str "PT" s))
duration (try-parse (str "P" s))]
duration
(f/fail "Unable to parse duration"))
```
Some context
https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang.CharSequence-
```
Examples:

"PT20.345S" -- parses as "20.345 seconds"
"PT15M" -- parses as "15 minutes" (where a minute is 60 seconds)
"PT10H" -- parses as "10 hours" (where an hour is 3600 seconds)
"P2D" -- parses as "2 days" (where a day is 24 hours or 86400 seconds)
"P2DT3H4M" -- parses as "2 days, 3 hours and 4 minutes"
"P-6H3M" -- parses as "-6 hours and +3 minutes"
"-P6H3M" -- parses as "-6 hours and -3 minutes"
"-P-6H+3M" -- parses as "+6 hours and -3 minutes"
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Search the existing try-all implementation and related failure combinators first, then compare their argument and return-value conventions with the proposed try-all-failed example. Define the behavior for trying each expression until one succeeds and returning the failure when all fail; done means the new combinator and similar variants follow the library's established API and are covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.