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

  • Home
  • SEARCH
  • 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 949587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:25:31+00:00 2026-05-15T23:25:31+00:00

I have just start reading Let over lambda and I thought I would try

  • 0

I have just start reading Let over lambda and I thought I would try and write a clojure version of the block-scanner in the closures chapter.

I have the following so far:

(defn block-scanner [trigger-string]
  (let [curr (ref trigger-string) trig trigger-string]
    (fn [data]
      (doseq [c data]
        (if (not (empty? @curr))
          (dosync(ref-set curr
           (if (= (first @curr) c)
             (rest @curr)
             trig)))))
      (empty? @curr))))
(def sc (block-scanner "jihad"))

This works I think, but I would like know what I did right and what I could do better.

  • 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-15T23:25:32+00:00Added an answer on May 15, 2026 at 11:25 pm

    I would not use ref-set but alter because you don’t reset the state to a completely new value, but update it to a new value which is obtained from the old one.

    (defn block-scanner
      [trigger-string]
      (let [curr (ref trigger-string)
            trig trigger-string]
        (fn [data]
          (doseq [c data]
            (when (seq @curr)
              (dosync
                (alter curr
                       #(if (-> % first (= c))
                          (rest %)
                          trig)))))
          (empty? @curr))))
    

    Then it is not necessary to use refs since you don’t have to coordinate changes. Here an atom is a better fit, since it can be changed without all the STM ceremony.

    (defn block-scanner
      [trigger-string]
      (let [curr (atom trigger-string)
            trig trigger-string]
        (fn [data]
          (doseq [c data]
            (when (seq @curr)
              (swap! curr
                     #(if (-> % first (= c))
                        (rest %)
                        trig))))
          (empty? @curr))))
    

    Next I would get rid of the imperative style.

    • it does more than it should: it traverses all data – even if we found a match already. We should stop early.
    • it is not thread-safe, since we access the atom multiple times – it might change in between. So we must touch the atom only once. (Although this is probably not interesting in this case, but it’s good to make it a habit.)
    • it’s ugly. We can do all the work functionally and just save the state, when we come to a result.
    (defn block-scanner
      [trigger-string]
      (let [state    (atom trigger-string)
            advance  (fn [trigger d]
                       (when trigger
                         (condp = d
                           (first trigger)        (next trigger)
                           ; This is maybe a bug in the book. The book code
                           ; matches "foojihad", but not "jijihad".
                           (first trigger-string) (next trigger-string)
                           trigger-string)))
            update   (fn [trigger data]
                       (if-let [data (seq data)]
                         (when-let [trigger (advance trigger (first data))]
                           (recur trigger (rest data)))
                         trigger))]
        (fn [data]
          (nil? (swap! state update data)))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer And there is the DBI-LINK that supports much more databases… May 16, 2026 at 2:07 pm
  • Editorial Team
    Editorial Team added an answer At first glance, Core Data is wonderful for your use… May 16, 2026 at 2:07 pm
  • Editorial Team
    Editorial Team added an answer You can combine namespaces into one name and use the… May 16, 2026 at 2:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Preface Let me start off by saying that I'm a relatively new programmer and
I'm reading Understanding Linux Kernel. Paging for 64-bit Architectures As we have seen in
I'm currently just starting to implement Dependency injection so I can start testing my
I was reading through some articles on Caching and Memoization and how to implement
For those of you who haven't been reading my Qt questoins, I am learning
I am not really sure what to do in this situation. I have some
I have a module which runs standalone in a JVM (no containers) and communicates
I have an MP3 audio file that I want to play from my Android
So thanks to many of the replies from this board, I have a much
Like most of us, I am a big fan of improving efficiency of code.

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.