Skip to main content

simple_to_compound

Function simple_to_compound 

Source
pub fn simple_to_compound(simple: K, enum_source: &str) -> K
Expand description

Convert a simple list to a compound list. Expected usage is to concatinate a simple list with a different type of list.

§Example

use kdbplus::*;
use kdbplus::api::*;

#[no_mangle]
pub extern "C" fn drift(_: K)->K{
  let simple=new_list(qtype::INT_LIST, 2);
  simple.as_mut_slice::<I>().copy_from_slice(&[12, 34]);
  let extra=new_list(qtype::COMPOUND_LIST, 2);
  extra.as_mut_slice::<K>().copy_from_slice(&[new_symbol("vague"), new_int(-3000)]);
  // Convert an integer list into a compound list
  let mut compound = simple_to_compound(simple, "");
  compound.append(extra).unwrap()
}

#[no_mangle]
pub extern "C" fn drift2(_: K)->K{
  let simple=new_list(qtype::ENUM_LIST, 2);
  simple.as_mut_slice::<J>().copy_from_slice(&[0_i64, 1]);
  // Convert an enum indices into a compound list while creating enum values from the indices which are tied with
  //  an existing enum variable named "enum", i.e., Enum indices [0, 1] in the code are cast into `(enum[0]; enum[1])`.
  let mut compound = simple_to_compound(simple, "enum");
  // Add `enum2[2]`.
  compound.push(new_enum("enum2", 2)).unwrap();
  compound.push(new_month(3)).unwrap();
  compound
}
q)drift: LIBPATH_ (`drift; 1);
q)drift2: LIBPATH_ (`drift2; 1);
q)drift[]
12i
34i
`vague
-3000i
q)enum: `mashroom`broccoli`cucumber
q)enum2: `mackerel`swordfish`tuna
q)drift2[]
`enum$`mashroom
`enum$`broccoli
`enum2$`tuna
2000.04m

§Note

  • To convert a list provided externally (i.e., passed from a q process), apply increment_reference_count before converting the list.
  • Enum elements from different enum sources must be contained in a compound list. Therefore this function intentionally restricts the number of enum sources to one so that user switches a simple list to a compound list when the second enum sources are provided.