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 8848955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:31:35+00:00 2026-06-14T12:31:35+00:00

I have a problem providing an edge case for the following recursive function that

  • 0

I have a problem providing an edge case for the following recursive function that searches hash maps and other similar key,value storage.

(def hashbrownies
  {"Mary","Dave"
   "Dave","Anne"
   "Anne","Tim"})

current approach

(defn recursive-lookup
  [key-lst search-func conditional]
  (let [next-key (search-func (first key-lst))]
    (if (conditional next-key)
      (reverse key-lst)
      (recur (cons next-key key-lst) search-func conditional))))

examples that are working

>> (recursive-lookup ["Mary"] #(hashbrownies %) (partial = nil))
=> ("Mary" "Dave" "Anne" "Tim")

>> (recursive-lookup ["Mary"] #(hashbrownies %) #(< (.length %) 4))
=> ("Mary" "Dave" "Anne")

Problematic:

>> (recursive-lookup ["Mary"] #(hashbrownies %) #(> (.length %) 4))
=> NullPointerException   clojure.lang.Reflector.invokeNoArgInstanceMember (Reflector.java:296)

I can see what the problem is: as the condition can not be met, the function #(> (.length %) 4) takes nil (the last possible return value) as an argument. But being new to Clojure I am not sure how to cover for this. Is there an idiomatic way?

solution:

(defn recursive-lookup
  [key-lst search-func conditional]
  (let [next-key (search-func (first key-lst))]
    (if (or (nil? next-key)
            (conditional next-key))
      (reverse key-lst)
      (recur (cons next-key key-lst) search-func conditional))))
  • 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-06-14T12:31:36+00:00Added an answer on June 14, 2026 at 12:31 pm

    You need to handle nil in your conditional function. You can use fnil for that. fnil replaces nil with some default value. So you can try:

    (fnil #(> (.length %) 4) "")
    

    If this conditional function receives nil it replaces nil with empty string “” and then call your function #(> (.length %) 4).

    Also you can use count instead .length. Count returns 0 for nil:

    (count "123") => 3
    (count nil) => 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have problem SIMILAR to preventing form data reposting, but not quite the same
I have a problem providing some third party librarys (JAR-files) I am using when
I have a problem making the link between the underlying socket (in this case,
Problem Description I have an abstract Paper class that contains common properties of all
The Android documentation being the mess that it is, I have big problem understanding
I'm providing a backlink selling service, but I have problem while checking the sold
that's my first post here. I have problem with managing activities. I have my
I have a problem with the following binding: <telerik:RadComboBox ItemsSource={Binding Source={StaticResource TemplateDataSource}, Path=Templates} SelectedValue={Binding
The basic problem I have here is that I have one xml file that

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.