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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:00:14+00:00 2026-05-31T15:00:14+00:00

I have a set of ints whose input I’d like to restrict. I would

  • 0

I have a set of ints whose input I’d like to restrict. I would like it to behave something like the following:

# RestrictedIntSet.add 15 (RestrictedIntSet.make 0 10)
Exception: 15 out of acceptable range [0 .. 10]

How can I implement this? In Java, it could look something like:

Set<Integer> restrictedSet = new HashSet<Integer>() {
    public boolean add(Integer i) {
        if (i < lowerBound || i > upperBound) {
            throw new IllegalArgumentException("out of bounds");
        }
        return super.add(i);
    }

Or, to be less abusing of inheritance:

public class RestrictedSet {

   private int lowerBound;
   private int upperBound;
   private Set elems = Sets.newHashSet();

   public RestrictedSet(int lowerBound, int upperBound) {
      this.lowerBound = lowerBound;
      this.upperBound = upperBound;
   }

   public boolean add(Integer i) {
      if (i < lowerBound || i > upperBound) {
         throw new IllegalArgumentException("out of bounds");
      }
      return elems.add(i);   
   }

   /* fill in other forwarded Set calls as needed */
}

What is the equivalent, idiomatic way to do this in OCaml?

  • 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-31T15:00:16+00:00Added an answer on May 31, 2026 at 3:00 pm

    Well, it depends, which set library are you using?

    Using the Set module of the standard library, you could do the following:

    module type RestrictedOrderedType = sig
      type t
      val compare : t -> t -> int
      val lower_bound : t
      val upper_bound : t
    end
    
    module RestrictedSet (Elem : RestrictedOrderedType) = struct
      include Set.Make(Elem)
    
      exception Not_in_range of Elem.t
    
      let check e =
        if Elem.compare e Elem.lower_bound < 0
         || Elem.compare e Elem.upper_bound > 0
        then raise (Not_in_range e)
        else e
    
      (* redefine a new 'add' in term of the one in Set.Make(Elem) *)
      let add e s = add (check e) s
    
      let singleton e = singleton (check e)
    end
    
    
    (* test *)
    module MySet = RestrictedSet(struct
      type t = int
      let compare = compare
      let lower_bound = 0
      let upper_bound = 10
    end)
    
    let test1 = MySet.singleton 3
    
    let test2 = MySet.add (-3) test1
    (* Exception: Not_in_range (-3) *)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview set up with a list of images like this: public
Not sure what is causing this, but I have two ints, like and dislike.
I would like the ability to set the systems ringtones from within my Android
I have data that is a set of ordered ints [0] = 12345 [1]
I have the following class public class Element { public List<int> Ints { get;private
Since batch doesn't really have ints, I have to work around it with set
We have set up a system where notifications get sent to a user with
I have set a canvas' background to an image of a company logo. I
I have set up transactional replication between two SQL Servers on different ends of
I have set up a version control system using TortoiseSVN at my home to

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.