@@ -437,7 +437,7 @@ defmodule Keyword do
437437 end
438438
439439 @ doc """
440- Gets the value from `key` and updates it, all in one pass.
440+ Gets the value for `key` and updates it in one pass, deleting duplicate keys .
441441
442442 The `fun` argument receives the value of `key` (or `nil` if `key`
443443 is not present) and must return a two-element tuple: the current value
@@ -483,7 +483,7 @@ defmodule Keyword do
483483 defp get_and_update ( [ { key , current } | t ] , acc , key , fun ) do
484484 case fun . ( current ) do
485485 { get , value } ->
486- { get , :lists . reverse ( acc , [ { key , value } | t ] ) }
486+ { get , :lists . reverse ( acc , [ { key , value } | delete ( t , key ) ] ) }
487487
488488 :pop ->
489489 { current , :lists . reverse ( acc , t ) }
@@ -509,7 +509,8 @@ defmodule Keyword do
509509 end
510510
511511 @ doc """
512- Gets the value under `key` and updates it. Raises if there is no `key`.
512+ Gets the value for `key` and updates it in one pass, deleting duplicate keys,
513+ raising if `key` can't be found in `keywords`.
513514
514515 The `fun` argument receives the value under `key` and must return a
515516 two-element tuple: the current value (the retrieved value, which can be
@@ -545,21 +546,21 @@ defmodule Keyword do
545546 get_and_update! ( keywords , key , fun , [ ] )
546547 end
547548
548- defp get_and_update! ( [ { key , value } | keywords ] , key , fun , acc ) do
549+ defp get_and_update! ( [ { key , value } | t ] , key , fun , acc ) do
549550 case fun . ( value ) do
550551 { get , value } ->
551- { get , :lists . reverse ( acc , [ { key , value } | delete ( keywords , key ) ] ) }
552+ { get , :lists . reverse ( acc , [ { key , value } | delete ( t , key ) ] ) }
552553
553554 :pop ->
554- { value , :lists . reverse ( acc , keywords ) }
555+ { value , :lists . reverse ( acc , t ) }
555556
556557 other ->
557558 raise "the given function must return a two-element tuple or :pop, got: #{ inspect ( other ) } "
558559 end
559560 end
560561
561- defp get_and_update! ( [ { _ , _ } = e | keywords ] , key , fun , acc ) do
562- get_and_update! ( keywords , key , fun , [ e | acc ] )
562+ defp get_and_update! ( [ { _ , _ } = h | t ] , key , fun , acc ) do
563+ get_and_update! ( t , key , fun , [ h | acc ] )
563564 end
564565
565566 defp get_and_update! ( [ ] , key , _fun , acc ) when is_atom ( key ) do
0 commit comments