#' Create a named list
#'
#' @param ... elements of the list to be created
#'
#' @details When ... contains objects with valid variable names,
#' those names will be used as the names of the objcts in the list.\cr
#'
#' @examples
#' a=1; b=1:2
#' named_list( a, a1=a, b, 3:4, d=5:6, "E" )
#'
#' @return A list.
#'
#' @export
#'

named_list <- function( ... ){
 # create a named list from ...
 # Shin-ichi Mayekawa
 # 20150803
 # bugfix: 20170513
 #

 # analyze ...
 namval=analyze_three_dots(...)

 if( length(namval) == 0 ) return(NULL)

 name=namval$name
 val=namval$val
 # Print(name,val)

 # values
 res=list()
 for( i in 1:length(val) ){
  res=append( res, list( val[[i]] ) )
 }

 # Print(name, !is.varname(name), val, names(val),length(names(val)) )
 # names
 nam=name
 if( length(names(val)) > 0 )
  nam[!is.varname(nam)]=names(val)[!is.varname(nam)]
 nam[!is.varname(nam)]=""

 names(res)=nam

 return( res )

} # named_list




#' Retrieve Objects from a Named List
#'
#' @param nl A named list
#'
#' @details
#' This function defines or replaces the objects in the parent frame. \cr
#'
#' mapply( assign, names(nl), nl, MoreArgs=list(envir=parent.frame()) )
#'
#'
#' @examples
#'
#' # in global env.:
#' a=1; a1=2; b=1:2
#' nl=named_list( a, a1=a+3, b, 3:4, d=5:6, "E" )
#' rmv(a,b,temp_var4,d,E)
#' from_named_list( nl )
#' Print(a,a1,b,temp_var4,d,E)
#'
#' # in a function:
#' f <- function(){
#'  nl=list(a=1,b=2)
#'  from_named_list( nl )
#'  Print("in function f:",a,b)
#' }
#' f()
#'
#' @export
#'

from_named_list <- function( nl ){
 # create a named list from ...
 # Shin-ichi Mayekawa
 # 20150803
 # bugfix: 20170513
 #

 name=names(nl)
 if( is.null(name) &&  is.vector(nl) ) return( nl )
 if( is.null(name) &&  is.list(nl) ){
  cat("\nerror1(from_named_list) The input list does not have name.\n")
  return( NULL )
 }

 nn=length(name)
 nam=paste("temp_var",1:nn, sep="")
 nam[is.varname(name)]=name[is.varname(name)]

 # ev=environment()
 # pev=parent.env(ev)
 pf=parent.frame()

 return( invisible( mapply( assign, nam, nl, MoreArgs=list(envir=pf) ) ) )

} # from_named_list


