All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scaloi.syntax.SetOps.scala Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2007 Learning Objects
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package scaloi
package syntax

import scala.collection.Set
import scala.language.implicitConversions

/**
  * Enhancements on sets.
  *
  * @param self the set
  * @tparam A the set value type
  */
final class SetOps[A](private val self: Set[A]) extends AnyVal {

  /**
    * Convert this set to a map with a function from keys to values.
    *
    * @param f a function from keys to values
    * @tparam B the value type
    * @return the resulting map
    */
  def mapTo[B](f: A => B): Map[A, B] = self.map(a => a -> f(a)).toMap

  /**
    * Test whether two sets intersect.
    * @param as the other set
    * @tparam AA the other set element type
    * @return whether the sets intersect
    */
  def intersects[AA <: A](as: Set[AA]): Boolean = as exists self.contains
}

/**
  * Implicit conversion for set operations.
  */
trait ToSetOps {

  /**
    * Implicit conversion from set to the set enhancements.
    * @param c the set
    * @tparam C its type
    */
  implicit def toSetOps[C](c: Set[C]): SetOps[C] = new SetOps(c)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy