Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6803759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:23:01+00:00 2026-05-26T19:23:01+00:00

Sorry if this question doesn’t make any sense. I’m currently in the midst of

  • 0

Sorry if this question doesn’t make any sense.

I’m currently in the midst of writing a small application and I want to ‘chain’ some functions together that form a wider computation.

So for example, I have a function that calls a webservice and returns a hash-map as a result, or nil if the was an error or something went wrong. The next function in the chain takes the map and does some further processing and then passes it on to the next function…and so on.

The thing is if the first function fails, it’s going to pass nil to the next function and that will throw a nullpointer (or whatever) and I don’t want it to even reach this stage.
So in other words I want the whole computation to fail if one item in the chain fails.

e.g.

;successful computation = function1 returns W -> function2 returns X -> function3 returns Y -> function4 returns Z -> Z
;failed computation = function1 returns W -> function2 returns X -> function3 returns nil -> nil
(-> (function1 "http://webservice.com") (function2) (function3) (function4))

The thing is I don’t want to be littering each of my functions with if(nil? arg) at the start, ideally I’d like an abstraction that could take care of that for me but I’m not really clued up on what’s available in Clojure

I was thinking of adopting the Scala style Option type that can either be Some(value) or None but I’d still need to litter my code with these checks at the start

Any ideas or replies would be really appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:23:02+00:00Added an answer on May 26, 2026 at 7:23 pm

    Clojure Contrib‘s algo.monad includes an implementation of the monad that Scala calls Option. It is called maybe-m and works by treating nil as None. You can use it like this:

    (ns mulk.monads-test
      (:refer-clojure)
      (:use clojure.repl
            clojure.algo.monads))
    
    (defn function1 [x]
      x)
    
    (defn function2 [x]
      (+ x 10))
    
    (defn function3 [x]
      (* x 2))
    
    (defn calc-stuff [thing]
      (domonad maybe-m
        [x1 (function1 thing)
         x2 (function2 x1)
         x3 (function3 (+ x2 10))]
        x3))
    
     (calc-stuff 10)   ;=> 60
     (calc-stuff nil)  ;=> nil
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very sorry if my question doesn't make sense as this is my
sorry I know this question was asked already but the solution doesn't work for
First of all, I am sorry if this question doesn't belong to SO since
Sorry for this newbie question, just got starting. I want a simple program to
Sorry for asking this simple question, but I seem not to find any answers.
Sorry if this question seems stupid, but it's stumped me for a couple days,
Sorry if this question is a bit open ended, but I'm pretty new to
Sorry for this question. There is a lot of information but unfortunately I haven't
Sorry if this question is too vague, but I'd rather not muddy it's point
Sorry if this question is very easy, but I can't find the answer anywhere.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.