Advertisement
Guest User

searchforwards

a guest
Mar 12th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.65 KB | None | 0 0
  1.   /**
  2.     *
  3.     * Find Forwards.  Starting at the current cursor position, locate the next
  4.     *
  5.     * occurrence of the character c. If the cursor reaches the end of the string
  6.     *
  7.     * without finding an occurrence of c then return false and return leave the
  8.     *
  9.     * cursor position unchanged. If the cursor finds an occurrence of c then leave
  10.     *
  11.     * the cursor at that position and return true.
  12.     *
  13.     * This operation does not change any variable other than the cursor position.
  14.     *
  15.     * Example:
  16.     *
  17.     * m i s s i s s i p p i
  18.     *
  19.     * ^                          cursor =  0
  20.     *
  21.     *
  22.     *
  23.     * Then perform  ff('i')
  24.     *
  25.     *
  26.     *
  27.     * m i s s i s s i p p i
  28.     *
  29.     *   ^                        cursor =  1: returns true
  30.     *
  31.     *
  32.     *
  33.     * Then perform  ff('i')
  34.     *
  35.     *
  36.     *
  37.     * m i s s i s s i p p i
  38.     *
  39.     *   ^                        cursor =  1: returns true
  40.     *
  41.     *
  42.     *
  43.     * Then perform  ff('p')
  44.     *
  45.     *
  46.     *
  47.     * m i s s i s s i p p i
  48.     *
  49.     *                 ^          cursor =  8: returns true
  50.     *
  51.     *
  52.     *
  53.     * Then perform  ff('s')
  54.     *
  55.     *
  56.     *
  57.     * m i s s i s s i p p i
  58.     *
  59.     * ^                          cursor = 0: returns false
  60.     *
  61.     */
  62.  
  63.   def ff( c: Char ): Boolean = {
  64.     var a = false
  65.     ( cursor until end ) collectFirst { case i if buffer( i ) == c => cursor = i; a = true }
  66.     a
  67.   }
  68.   /*  var b = false
  69.       ( cursor until end )
  70.         .withFilter( x => !b )
  71.         .withFilter( x => buffer( x ) == c )
  72.         .foreach( x => {
  73.           b = true
  74.           cursor = x
  75.         } )
  76.       b  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement